Hono

02 Setup Anvia

Create a reusable Anvia agent module for Hono routes.

Create provider clients, models, and shared tools outside the Hono handler when their configuration is the same for every request.

1. Create src/ai/support-agent.ts

import { AgentBuilder, createTool } from "@anvia/core";
import { OpenAIClient } from "@anvia/openai";
import { z } from "zod";

const apiKey = process.env.OPENAI_API_KEY;

if (!apiKey) {
  throw new Error("OPENAI_API_KEY is required");
}

const client = new OpenAIClient({ apiKey });
export const model = client.completionModel("gpt-5.5");

const lookupPolicy = createTool({
  name: "lookup_policy",
  description: "Look up a short support policy by key.",
  input: z.object({
    key: z.enum(["password_reset", "priority_support"]),
  }),
  output: z.object({
    text: z.string(),
  }),
  async execute({ key }) {
    const policies = {
      password_reset: "Password reset links expire after 30 minutes.",
      priority_support: "Enterprise customers receive priority support.",
    };

    return { text: policies[key] };
  },
});

export const supportAgent = new AgentBuilder("support", model)
  .instructions("Answer support questions clearly. Use tools for policy facts.")
  .tool(lookupPolicy)
  .defaultMaxTurns(3)
  .build();

2. Create src/app.ts

import { Hono } from "hono";

export const app = new Hono();

app.get("/health", (c) => c.json({ ok: true }));

3. Create src/server.ts

import { serve } from "@hono/node-server";
import { app } from "./app";

serve({
  fetch: app.fetch,
  port: 3000,
});

Run it:

pnpm exec tsx src/server.ts

Next

Expose the agent through a JSON route in Route Handler. Related guides: Creating Agents, Tools, and Provider Clients.