## Overview

The agent verb orchestrates a complete voice AI agent by wiring together three separate components — STT, LLM, and TTS — with integrated turn detection. Unlike the s2s verbs (where a single vendor handles everything), the agent verb lets you mix and match: e.g. Deepgram for STT, Anthropic for the LLM, and Cartesia for TTS.

The agent manages the full conversational turn cycle:
1. User speaks → STT produces a transcript
2. Turn detection decides the user is done speaking
3. Transcript is sent to the LLM
4. LLM response tokens stream to TTS
5. TTS audio plays back to the caller
6. If the user barges in, TTS stops and a new turn begins

## Turn detection

The `turnDetection` property controls how the agent decides the user has finished speaking.

**`"stt"` (default)** — Uses the STT vendor's native end-of-utterance signal. For most vendors this is silence-based. Some vendors have smarter built-in turn detection:
- **deepgramflux** — Acoustic + semantic turn detection (Deepgram's "Flux" model)
- **assemblyai** — Native turn-taking with `u3-rt-pro` model
- **speechmatics** — Built-in turn detection

These vendors always use their native detection regardless of the `turnDetection` setting.

**`"krisp"`** — Uses the Krisp acoustic end-of-turn model, which analyzes speech patterns rather than just silence. Good for natural conversation where users pause mid-thought. Can be tuned:

```json
{
  "turnDetection": {
    "mode": "krisp",
    "threshold": 0.3
  }
}
```

Lower threshold = faster turn transitions (more aggressive). Default is 0.5.

**IMPORTANT NOTE**: you must have a krisp API key in order to utilize this module on a self-hosted jambonz system.  Please contact us at support@jambonz.org if you need more details.

## Early generation (speculative preflight)

Early generation speculatively sends the transcript to the LLM *before* end-of-turn is confirmed. If the transcript matches when the turn does end, buffered tokens are released immediately — shaving off the LLM prompt time. If the user keeps talking and the transcript changes, the speculative response is discarded. This is a latency optimization with no correctness downside.

There are two ways early generation is triggered:

- **Krisp turn detection** — Set `earlyGeneration: true` to opt in. Krisp emits an early signal that triggers the speculative LLM prompt before final end-of-turn confirmation.
- **Deepgram Flux** — Early generation happens automatically. Flux emits a native `EagerEndOfTurn` event that triggers preflight regardless of the `earlyGeneration` setting.

For other STT vendors with native turn-taking (assemblyai, speechmatics), early generation is not available — they don't emit a preflight signal.

## Noise isolation

The `noiseIsolation` property enables server-side noise cancellation on the call audio. By default it filters the inbound (caller) audio, improving STT accuracy in noisy environments. It can also be configured to filter outbound audio via the `direction` option. Three vendors are available:

- **`"krisp"`** — Krisp's proprietary noise cancellation. Requires a Krisp API key on self-hosted systems.
- **`"rnnoise"`** — Open-source RNNoise-based noise cancellation. No API key required.
- **`"kugelaudio"`** — KugelAudio Clarity (`clarity-1`) cloud speech enhancement. Uses a KugelAudio speech credential with "use for noise isolation" enabled (pick one with `label` in the object form, or pass `auth: {apiKey, apiUri}` inline); adds a short delay (the round trip to KugelAudio) to the filtered audio. `level` is ignored.

Shorthand (default settings):

```json
{
  "noiseIsolation": "krisp"
}
```

Detailed configuration:

```json
{
  "noiseIsolation": {
    "mode": "krisp",
    "level": 80,
    "direction": "read"
  }
}
```

- `mode` — Vendor: `"krisp"` or `"rnnoise"`.
- `level` — Suppression level 0–100. Higher values are more aggressive. Default: 100.
- `direction` — `"read"` filters caller audio (default), `"write"` filters outbound audio.
- `model` — Optional model name override.

Noise isolation can also be enabled/disabled mid-call via the `config` verb, the REST LCC API, or a WebSocket inject command (`noiseIsolation:status`).

## Barge-in

By default, users can interrupt the assistant while it's speaking. The `bargeIn` object controls this:

```json
{
  "bargeIn": {
    "enable": true,
    "minSpeechDuration": 0.5,
    "sticky": false
  }
}
```

- `minSpeechDuration` — Seconds of speech required to confirm an interruption. Prevents brief noises from cutting off the assistant. Default: 0.5.
- `sticky` — If true, once the user interrupts, the assistant does not resume speaking the interrupted response.

## eventHook events

The `eventHook` receives real-time events during the conversation. In WebSocket mode, listen for these with `session.on('/your-event-hook', handler)`.

| Event type | Description | Key fields |
|---|---|---|
| `user_transcript` | User speech recognized | `transcript` |
| `llm_response` | Assistant reply text | `response` |
| `user_interruption` | User barged in | `cause` (`vad`, `transcript`, `interrupt_prediction`), `probability`, `spoken_text`, `spoken_estimated` |
| `overlapping_speech` | Caller spoke over the reply without interrupting it | `outcome` (`ignored`, `held`, `answered`), `transcript`, `word_count` |
| `interrupt_prediction_ignored` | Interrupt prediction before the assistant spoke | `probability`, `state` |
| `no_response_timeout` | Caller silent; the agent prompts them | `count`, `timeout_ms` |
| `llm_error` | An LLM request failed | `stage` (`prompt`, `retry_without_tools`, `tool_result`), `vendor`, `model`, `message`, `retrying` |
| `turn_end` | End-of-turn summary | `transcript`, `response`, `interrupted`, `latency` |

The `turn_end` event is the most useful for observability. It includes per-component latency metrics (STT, LLM, TTS) in milliseconds. See the `callback:agent-turn` schema for the full payload structure.

## toolHook (function calling)

> **Before implementing a custom toolHook handler**, check if `@jambonz/tools` provides a pre-built tool that fits your needs (weather, web search, calculator, date/time, Wikipedia). Use `createWeather()`, `createTavilySearch()`, etc. with `registerTools()` to avoid hand-rolling common functionality. Call `list_jambonz_tools` in the MCP server for the full catalog.

When the LLM requests a tool/function call, the agent sends a request to the `toolHook` with:

```json
{
  "tool_call_id": "call_abc123",
  "name": "get_weather",
  "arguments": { "city": "Portland" }
}
```

The `arguments` field is already parsed (an object, not a JSON string).

**Webhook response**: Return the tool result in the HTTP response body as JSON. The result is stringified and fed back to the LLM.

**WebSocket**: The tool call arrives as an event on the hook path. Respond by calling `session.toolOutput(tool_call_id, result).reply()`.

## MCP servers (external tools)

Instead of (or in addition to) defining tools inline via `llmOptions.tools` and handling them with `toolHook`, you can connect to external MCP servers. The agent connects to each server at startup via SSE transport, discovers available tools, and makes them available to the LLM alongside any inline tools.

```json
{
  "verb": "agent",
  "mcpServers": [
    {
      "url": "https://livescoremcp.com/sse"
    }
  ],
  "llm": {
    "vendor": "openai",
    "model": "gpt-4.1",
    "llmOptions": {
      "messages": [
        { "role": "system", "content": "You are a sports assistant. Use available tools to look up live scores and fixtures when asked." }
      ]
    }
  },
  "stt": { "vendor": "deepgram", "language": "en-US" },
  "tts": { "vendor": "cartesia", "voice": "sonic-english" }
}
```

The [LiveScore MCP server](https://livescoremcp.com/) is a free, public MCP server that exposes tools for live football scores, fixtures, team stats, and player data. The agent discovers these tools automatically at startup — no need to define tool schemas in `llmOptions.tools`. A caller can simply ask "what football matches are on right now?" and the LLM will use the `get_live_scores` tool to fetch real-time data.

If an MCP server requires authentication, pass credentials in the `auth` property:

```json
{
  "mcpServers": [
    {
      "url": "https://mcp.example.com/sse",
      "auth": {
        "apiKey": "your-api-key-here"
      }
    }
  ]
}
```

**How tool dispatch works**: When the LLM requests a tool call, the agent checks MCP servers first. If the tool name matches one discovered from an MCP server, the call is dispatched there directly and the result is fed back to the LLM. If no MCP server provides the tool, it falls through to the `toolHook` webhook. You can use both MCP servers and `toolHook` together — MCP handles the tools it knows about, and `toolHook` handles the rest.

**TypeScript example** — an agent with the LiveScore MCP server:

```typescript
session
  .agent({
    stt: { vendor: 'deepgram', language: 'en-US' },
    tts: { vendor: 'cartesia', voice: 'sonic-english' },
    llm: {
      vendor: 'openai',
      model: 'gpt-4.1',
      llmOptions: {
        messages: [
          { role: 'system', content: 'You are a sports assistant. Use available tools to answer questions about football scores, fixtures, and teams.' },
        ],
      },
    },
    mcpServers: [
      { url: 'https://livescoremcp.com/sse' },
      // To use a server that requires auth:
      // { url: 'https://mcp.example.com/sse', auth: { apiKey: 'your-key' } },
    ],
    turnDetection: 'krisp',
    actionHook: '/agent-complete',
  })
  .send();
```

## Mid-conversation updates

The agent supports asynchronous updates while a conversation is in progress. These let you change the agent's behavior, inject new context, modify available tools, or trigger a new LLM response — without interrupting the current verb stack.

Updates can be sent via:
- **WebSocket**: `session.updateAgent(data)` (sends an `agent:update` command)
- **REST API**: `client.calls.updateAgent(callSid, data)` (sends `agent_update` in the PUT body)

### update_instructions

Replace the LLM system prompt mid-conversation. Useful when the conversation transitions to a different topic or agent persona.

```typescript
// WebSocket
session.updateAgent({
  type: 'update_instructions',
  instructions: 'You are now a billing support agent. Help the caller with invoice questions.',
});

// REST
await client.calls.updateAgent(callSid, {
  type: 'update_instructions',
  instructions: 'You are now a billing support agent. Help the caller with invoice questions.',
});
```

### inject_context

Append messages to the LLM conversation history. Useful for injecting CRM data, call notes, or other context retrieved after the call started.

```typescript
session.updateAgent({
  type: 'inject_context',
  messages: [
    { role: 'system', content: 'Customer account #12345: Gold tier, 3 open tickets.' },
  ],
});
```

### update_tools

Replace the tool set available to the LLM. The new tools take effect on the next LLM turn.

```typescript
session.updateAgent({
  type: 'update_tools',
  tools: [
    {
      type: 'function',
      function: {
        name: 'transfer_call',
        description: 'Transfer the caller to a specialist',
        parameters: { type: 'object', properties: { department: { type: 'string' } } },
      },
    },
  ],
});
```

### update_emotion

Change the `emotion` settings of an agent that has them. Fields you leave out keep their value; the system prompt is updated in place.

```typescript
session.updateAgent({
  type: 'update_emotion',
  emotion: { emotions: ['calm', 'empathetic'], sounds: false },
});
```

### generate_reply

Prompt the LLM to generate a new response. If the agent is currently idle, the prompt executes immediately. If the agent is busy (e.g. the assistant is speaking), the request is queued and executes when the current turn completes.

Use `interrupt: true` to cancel the current response and generate immediately — useful for supervisor overrides or urgent context changes.

```typescript
// Simple prompt
session.updateAgent({
  type: 'generate_reply',
  user_input: 'The customer just entered their account number: 12345',
});

// With one-shot instructions
session.updateAgent({
  type: 'generate_reply',
  user_input: 'Customer is asking about refunds',
  instructions: 'Be empathetic and offer a 20% discount before processing a refund.',
});

// Interrupt current response
session.updateAgent({
  type: 'generate_reply',
  user_input: 'Urgent: supervisor override',
  interrupt: true,
});
```

## Your own harness (`llm.vendor: "harness"`)

With `vendor: "harness"` the customer's own code decides what the agent says, while jambonz keeps STT, turn-taking, barge-in, TTS and the turn record. feature-server opens one WebSocket per call, with subprotocol `harness.jambonz.org`, to `harness.url` when the verb gives one, otherwise to the URL in the account's **Harness** LLM credential (fields `url` and `api_key`, looked up by vendor and `label` like any LLM credential). The key comes from that credential and is sent as `Authorization: Bearer <api_key>`. With `harness.url`, a key is sent only from the credential named by `label`; with no `label` the socket opens without one.

```json
{
  "verb": "agent",
  "stt": {"vendor": "deepgramflux", "language": "en-US"},
  "tts": {"vendor": "cartesia", "voice": "..."},
  "llm": {
    "vendor": "harness",
    "label": "my-harness",
    "llmOptions": {"tools": [{"name": "lookup_account", "description": "...", "parameters": {"type": "object"}}]},
    "harness": {
      "url": "wss://harness.example.com/agent",
      "metadata": {"agent": "outage-line"},
      "firstChunkTimeoutMs": 4000,
      "holdingPhrase": "One moment.",
      "onHarnessFailure": {"say": "Sorry, I'm having trouble. Transferring you now.", "transfer": "+15085550100"}
    }
  },
  "toolHook": "/tool",
  "eventHook": "/event",
  "actionHook": "/done"
}
```

The harness owns prompts, context, fillers and reprompts, so with it `llmOptions` may carry only `tools`, and `model`, `auth`, `connectOptions`, `mcpServers`, `toolFiller`, `earlyGeneration: true`, `speechCleanup: true` and `noResponseMaxPrompts` are rejected by the schema. The tools are sent to the harness in `session:start` as OpenAI function definitions; when the harness sends `tool:call`, jambonz runs it through the `toolHook` exactly as for a model (`agent:tool-call`, answered with `llm:tool-output`) and returns `tool:result`. `handoff`, when present, gives the defaults for transfers the harness asks for.

What jambonz sends: `session:start`, `speech:start`, `transcript`, `turn:start` (`input` is `speech`, `greeting` or `no_input`), `turn:cancel`, `event` (`user_interruption`, `reply_spoken`, `reply_dropped`, `silence`, `transfer`, `recording`, `application_update`, `error`), `command:result`, `tool:result`, `session:end`. What the harness sends: `reply:chunk` / `reply:done` (OpenAI `ChatCompletionChunk`s; `choices[0].delta.content` is spoken), `tool:call`, and `command` (`hangup`, `transfer`, `record`, `barge_in`, `listen`). The full protocol is the harness protocol reference.

Notes on this implementation:
- `application_update` carries the application's `agent:update` type as `update_type` (the message's own `type` is `event`), with the rest of the update in `data`.
- `user_interruption.spoken` is exact with TTS vendors that report alignment, and estimated from playout time otherwise.
- `noResponseTimeout` sends a `silence` event (`seconds`, `streak`) each time it elapses; jambonz does not reprompt.
- `play` and `listen` with `hints` are not supported yet and are refused in `command:result`; `listen` with `language` works where the STT vendor can change language mid-call (Deepgram Flux).
- Failures (socket cannot open, no reply twice in `firstChunkTimeoutMs`, a dropped socket that cannot be reopened within 2 s) run `onHarnessFailure`; the actionHook reports `completionReason: "harness_failure"` with `harness_error: {reason, message}` and the eventHook gets `harness_failure`.
- Each `turn_end` carries `engine: "harness"`, `harness.turn_record` (the harness's `reply:done.turn_record`, up to 16 KB) and `latency.harness_first_chunk_ms` in place of `llm_ms`.

## LLM configuration

The `llm` property is the only required field. It configures the text-to-text LLM:

```json
{
  "llm": {
    "vendor": "openai",
    "model": "gpt-4.1",
    "llmOptions": {
      "messages": [
        { "role": "system", "content": "You are a helpful voice assistant." }
      ],
      "tools": [
        {
          "type": "function",
          "function": {
            "name": "get_weather",
            "description": "Get current weather for a city",
            "parameters": {
              "type": "object",
              "properties": {
                "city": { "type": "string" }
              },
              "required": ["city"]
            }
          }
        }
      ]
    }
  }
}
```

For Anthropic models, use `"vendor": "anthropic"` and structure messages accordingly (Anthropic uses `"role": "user"` for the system-level instruction).

### Vendor-specific parameters (`vendorParams`)

`llmOptions.vendorParams` passes vendor-specific request parameters that jambonz has no first-class option for. For example, OpenRouter's provider routing and fallback models:

```json
{
  "llm": {
    "vendor": "openrouter",
    "model": "meta-llama/llama-3.3-70b-instruct",
    "llmOptions": {
      "systemPrompt": "You are a helpful voice assistant.",
      "vendorParams": {
        "provider": { "sort": "latency", "data_collection": "deny" },
        "models": ["openai/gpt-4.1-mini", "google/gemini-3.5-flash"]
      }
    }
  }
}
```

- Fields are added to the vendor request: the request body for OpenAI-compatible vendors (openai, openrouter, groq, deepseek, xai, azure-openai, ...) and the generation config for `google` and `vertex-gemini`. `anthropic` and `bedrock` ignore it.
- Add-only: a field jambonz already sets (`model`, `messages`, `tools`, max tokens, `temperature`, ...) is never overridden, so use the first-class option for those.
- Not validated by jambonz. A misspelled field, or one the vendor rejects, surfaces as a vendor error on the first turn.

## Greeting

By default (`greeting: true`), the agent prompts the LLM to generate an initial greeting before the user speaks. Set `greeting: false` if you want the agent to wait silently for the user to speak first.

## Controlling what is spoken

Everything the LLM writes is normally sent to TTS. Two options change that. Neither changes what the LLM sees: conversation history, `llm_response` events and the `turn_end` transcript always hold the model's raw text.

**`speechCleanup`** (default `true`) drops text that models write but don't mean to be heard:

- a paragraph that is only a stage direction in `()`, `[]` or `**`, such as `(silence)`, `(empty response)` or `[No response needed - the call has ended]`. Models write these when they are told to stay silent. A bracketed aside inside a sentence, such as "PPO (including Anthem)", is still spoken, and so is a span with no letters in it, such as a phone number's area code.
- `<think>`, `<thinking>` and `<reasoning>` blocks, wherever they appear.

The cleanup runs on the streaming tokens and holds back only text that could still turn out to be one of these, so ordinary speech is not delayed. If nothing is left to say, the reply ends without speaking, the same way as a reply that only called tools. Set `speechCleanup: false` to send the text to TTS exactly as written.

**`emotion`** makes the agent hear the caller's tone and change its own voice style. Omit it and the agent behaves exactly as without it.

```json
"emotion": {
  "listen": "auto",
  "speak": true,
  "emotions": ["neutral", "happy", "empathetic", "calm"],
  "sounds": false,
  "minConfidence": 0.5,
  "default": "neutral"
}
```

- **Hearing.** When the STT vendor reports the caller's emotion during streaming — modulate (`velma-2-stt-streaming`; not `velma-2-stt-streaming-english-v2`), smallestai (`pulse-2`) or inworld (voice profile) — the user message the LLM receives starts with `[caller tone: angry]`. With `listen: "auto"` (the default) and `"stt"` the agent turns on that vendor's emotion output unless your recognizer options already set it. Without a label the LLM is told to judge the tone from the words (`auto` and `llm`). Labels: `neutral happy sad angry fearful surprised disgusted calm other`. Reprompts and other prompts the agent writes itself are never prefixed. When the vendor's word is more specific than the label (modulate `Frustrated`, inworld `tender`), the LLM gets that word and the event carries it as `emotion.detail`. Not reported in streaming by the other STT vendors: assemblyai (sentiment is a pre-recorded add-on only), deepgram (sentiment is batch only), speechmatics (laughter and applause events only), gladia (text sentiment arrives as a separate message the agent does not read yet), aws (sentiment only on the Call Analytics API, not the one jambonz uses), and elevenlabs, soniox, google, microsoft and the rest. With them the LLM judges the tone from the words.
- **Speaking.** The LLM may start a sentence with a directive such as `<voice emotion="empathetic" speed="slow"/>` (attributes: `emotion`, `intensity`, `speed`, `volume`, `sound`). The directive is never spoken: it is rendered as the TTS vendor's own tag in front of that sentence, so the style can change mid-reply on the same connection, and lasts until the next directive or the end of the reply. Each reply starts in `default`. The system prompt only offers what the configured TTS model can render:

| TTS vendor and model | Styles |
| --- | --- |
| elevenlabs `eleven_v3*`, `eleven_v4*` | emotions, soft/loud, sounds (use a v3 voice) |
| inworld `inworld-tts-2` (`inworld-tts-2-flash`: sounds only) | emotions, speed, volume, sounds |
| fishaudio `s2*` | emotions, soft/loud, laugh, sigh |
| soniox `tts-rt-v2` | emotions, speed, soft/loud, sounds |
| paxa `paxa-tts-flash-v2` | emotions, speed, volume, sounds |
| cartesia `sonic-3` and later (`sonic-3.5`, `sonic-3.6`) | emotions (English), speed, volume, laughter |
| minimax `speech-2.8*` | sounds only (laugh, sigh, breath) |
| xai | sounds only (laugh, sigh, breath, pause) |

Checked against each vendor's documentation and not supported on the streaming connection the agent uses: assemblyai (no emotion parameter; bracketed tags are read aloud), bland (its `[say …]` tags made no audible difference over its streaming socket in live tests), deepgram Aura and deepgramflux (`expressivity` is fixed for the connection), google (Chirp 3 HD has no style control; Gemini-TTS tags are not on the streaming path), hume (acting `description` per request, not yet per sentence), camb (its streaming model takes sound tags only), minimax emotion (fixed at connect; only its sound tags change), murf and sarvam (style fixed per voice or connection), nvidia Magpie (emotion is part of the voice name), rimelabs (Coda takes tags only as best-effort cues), elevenlabs Flash/Turbo/Multilingual, asyncai, gradium, kugelaudio, neuphonic, nineninesix, respeecher, smallestai and svara. With these the voice keeps the style it was configured with.

  With any other vendor or model no voice guidance is added and any directive the LLM writes anyway is removed. Invalid directives are removed, never spoken.
- **Events.** With `emotion`, `llm_response.response` and the `turn_end` response hold the text without directives, and `llm_response` adds `styles` (`[{offset, emotion, ...}]`). `user_transcript` carries `emotion` (`{label, detail, confidence, source}`); `turn_end` and the session data carry `user_emotion` and `agent_styles`, and `latency.first_speech_token_ms` (prompt to the first spoken token). Conversation history keeps the directives.

Change the settings mid-call with `update_emotion` (see below).

**`speak: false`** keeps all of the agent's text out of TTS. Use it for an agent that must never talk, such as one that navigates a DTMF-only phone menu by calling tools. The LLM's replies are still kept, so the agent can take notes for itself. With `speak: false`, `toolFiller` and `earlyGeneration` are turned off.

```json
{
  "verb": "agent",
  "speak": false,
  "llm": {
    "vendor": "anthropic",
    "model": "claude-haiku-4-5",
    "llmOptions": {
      "messages": [
        { "role": "system", "content": "You navigate phone menus by pressing keys with the send_dtmf tool." }
      ]
    }
  }
}
```

## Complete example (TypeScript)

A voice agent using Deepgram STT, OpenAI LLM, and Cartesia TTS with Krisp turn detection. Exposes multiple endpoints with different STT/TTS combinations:

```typescript
import * as http from 'node:http';
import { createEndpoint, Session } from '@jambonz/sdk/websocket';

const envVars = {
  OPENAI_MODEL: {
    type: 'string' as const,
    description: 'OpenAI model to use',
    default: 'gpt-4.1-mini',
  },
  SYSTEM_PROMPT: {
    type: 'string' as const,
    description: 'System prompt for the voice agent',
    uiHint: 'textarea' as const,
    default: 'You are a helpful voice AI assistant. Your responses are concise and conversational.',
  },
};

function handleSession(session: Session) {
  const model = session.data.env_vars?.OPENAI_MODEL || 'gpt-4.1-mini';
  const systemPrompt = session.data.env_vars?.SYSTEM_PROMPT || envVars.SYSTEM_PROMPT.default;

  session.on('/agent-event', (evt: Record<string, unknown>) => {
    if (evt.type === 'turn_end') {
      const { transcript, response, interrupted, latency } = evt as Record<string, unknown>;
      console.log('turn_end', JSON.stringify({ transcript, response, interrupted, latency }, null, 2));
    }
  });

  session.on('/agent-complete', () => {
    session.hangup().reply();
  });

  session
    .agent({
      stt: {
        vendor: 'deepgram',
        language: 'multi',
        deepgramOptions: { model: 'nova-3-general' },
      },
      tts: {
        vendor: 'cartesia',
        voice: '9626c31c-bec5-4cca-baa8-f8ba9e84c8bc',
      },
      llm: {
        vendor: 'openai',
        model,
        llmOptions: {
          messages: [{ role: 'system', content: systemPrompt }],
        },
      },
      turnDetection: 'krisp',
      earlyGeneration: true,
      bargeIn: { enable: true },
      eventHook: '/agent-event',
      actionHook: '/agent-complete',
    })
    .send();
}

const port = parseInt(process.env.PORT || '3000', 10);
const server = http.createServer();
const makeService = createEndpoint({ server, port, envVars });

const svc = makeService({ path: '/' });
svc.on('session:new', (session) => handleSession(session));
```

## Complete example (JavaScript)

The same agent in plain JavaScript:

```javascript
const http = require('node:http');
const { createEndpoint } = require('@jambonz/sdk/websocket');

const envVars = {
  OPENAI_MODEL: {
    type: 'string',
    description: 'OpenAI model to use',
    default: 'gpt-4.1-mini',
  },
  SYSTEM_PROMPT: {
    type: 'string',
    description: 'System prompt for the voice agent',
    uiHint: 'textarea',
    default: 'You are a helpful voice AI assistant. Your responses are concise and conversational.',
  },
};

function handleSession(session) {
  const model = session.data.env_vars?.OPENAI_MODEL || 'gpt-4.1-mini';
  const systemPrompt = session.data.env_vars?.SYSTEM_PROMPT || envVars.SYSTEM_PROMPT.default;

  session.on('/agent-event', (evt) => {
    if (evt.type === 'turn_end') {
      const { transcript, response, interrupted, latency } = evt;
      console.log('turn_end', JSON.stringify({ transcript, response, interrupted, latency }, null, 2));
    }
  });

  session.on('/agent-complete', () => {
    session.hangup().reply();
  });

  session
    .agent({
      stt: {
        vendor: 'deepgram',
        language: 'multi',
        deepgramOptions: { model: 'nova-3-general' },
      },
      tts: {
        vendor: 'cartesia',
        voice: '9626c31c-bec5-4cca-baa8-f8ba9e84c8bc',
      },
      llm: {
        vendor: 'openai',
        model,
        llmOptions: {
          messages: [{ role: 'system', content: systemPrompt }],
        },
      },
      turnDetection: 'krisp',
      earlyGeneration: true,
      bargeIn: { enable: true },
      eventHook: '/agent-event',
      actionHook: '/agent-complete',
    })
    .send();
}

const port = parseInt(process.env.PORT || '3000', 10);
const server = http.createServer();
const makeService = createEndpoint({ server, port, envVars });

const svc = makeService({ path: '/' });
svc.on('session:new', (session) => handleSession(session));

console.log(`jambonz voice agent listening on port ${port}`);
```
