Docs

Tool calling (function calling)

Function calling for a live AI avatar: it calls functions you register in your page, or your server runs the agent loop. The platform never executes your tools.

Function calling during a live call: the avatar calls functions you register in your page (attachAvatarTools from realtime-avatar/tools, or the useCharacterTools hook from realtime-avatar/react) once your server turns on clientTools: true, or your server runs the agent loop and your page hands her the result with sendTurn. Your tools never run on the platform. There is no hosted executor: a tool executes in your page or on your server, with your credentials, under your authorization rules. That is a deliberate boundary, not a gap we forgot to fill.

The reason is trust. A hosted tool runner would need your credentials, your authorization rules, and a say in when a side effect fires. Your backend already has all three and already knows which user is on the call. So execution stays where the trust already is, in two lanes: client tools in your page and server steering from your backend.

Client tools: functions she can call in your page

Opt in at mint with clientTools: true (wire capabilities: ["client_tools"]) — part of the call policy, so it is your server's decision like everything else — then declare the tools in the page after the room connects. The manifest is registered over RPC; nothing about a tool ever appears in the mint request, and the tool name is the record key.

// Server — the connect route opts the call in. Policy, like everything else.
session: async () => ({ instructions, clientTools: true }),

// Page — after room.connect(). The name she calls is the record key.
import { attachAvatarTools, type AvatarTool } from "realtime-avatar/tools";

const check_order: AvatarTool<{ order_id: string }> = {
  description: "Look up the status of an order by id",
  parameters: {
    type: "object",
    properties: { order_id: { type: "string" } },
    required: ["order_id"],
  },
  execute: async ({ order_id }) =>
    await fetch("/api/orders/" + order_id).then((r) => r.json()),
};

const { accepted, rejected } = await attachAvatarTools(room, { check_order });

A tool has 2.5 seconds. That is a conversational floor, not a tunable — past a couple of seconds of dead air the call stops feeling live. The platform abandons the call after the deadline and tells her it failed, and the abort is cooperative: every execute receives an AbortSignal, but a handler that ignores it still runs to completion and can commit a side effect — only its result is discarded. Make anything slow idempotent, or check signal.aborted before you write. A tool that calls a model does not fit: return an acknowledgement inside the deadline and deliver the real answer on screen when it lands. And read the { accepted, rejected } return — a rejected schema is a tool she simply does not have.

Server steering: the two primitives

CallWhat happensUse for
sendTurn(text, { instructions })Interrupts stale speech and generates a reply to text, with instructions applied to this turn only — passed as generation instructions, not concatenated into the user message.Injecting a tool result and letting the character deliver it in her own voice
sendClosingTurn(text)Speaks text verbatim — never routed through the model — uninterruptibly, then ends the session.The one line that must be exact: a goodbye, a legal disclosure

Per-turn instructions is the tool-result channel. It steers wording, register, and content for one reply and then evaporates — it does not mutate the session prompt, so a tool result cannot leak into every later turn.

The loop

Run your agent where your data is: on your server. The call is the interface, not the brain.

// 1. You observe the user's turn. 2. Your agent decides a tool is needed
// and runs it. 3. You hand the RESULT back and let her say it.
const result = await tools.checkOrderStatus({ userId, orderId });

await session.sendTurn(userText, {
  instructions: [
    "Answer using ONLY these facts:",
    JSON.stringify(result),
    "One or two spoken sentences. Do not read the JSON aloud.",
  ].join("\n"),
});
import json
from fastapi import Depends

# The live turn is client-side, so Python's role is to DECIDE and to receive.
# Your client calls your endpoint; you run the tool and return the steer text.
@app.post("/api/turn")
def turn(req: TurnRequest, user=Depends(current_user)):
    result = check_order_status(user.id, req.order_id)
    return {
        "text": req.text,
        "steer": (
            "Answer using ONLY these facts: "
            f"{json.dumps(result)}. "
            "One or two spoken sentences. Do not read the JSON aloud."
        ),
    }

The client passes that straight into sendTurn(text, { instructions: steer }). Keeping the decision on your server is the point — the credentials and the authorization rules already live there.

For a deterministic string — a confirmation number, a price, a required disclosure — do not route it through the model at all. Use the verbatim path so what she says is exactly what you wrote.

Latency

A tool call between the user finishing a sentence and the character starting to speak is dead air, and dead air on a video call is louder than on a chat. Two things help:

  • Speak first, then resolve. Send a short acknowledging turn immediately ("let me look"), run the tool, then send the answer turn.
  • Pre-fetch at mint. Anything you can know before the call starts belongs in the policy's context (wire initial_context), not in a tool call three seconds in.

Getting the conversation back

Your agent needs to know what was said — and what she did. Register a transcript webhook at mint time (wire transcript_webhook: an https URL and a 16–200 character secret; tag the session with client_metadata) and you get a signed POST after the call ends with the full two-sided transcript (segments) and the tool calls the model acted on (tool_calls: name, arguments, result or error, and duration). It is sent after capacity is released, so it never delays the next caller. Delivery is at-least-once: the worker waits 5 seconds for your answer, retries once after ~2 seconds on a 5xx or a timeout with the identical body and signature, and takes any status under 500 as delivered. A session with no committed turns and no tool calls sends nothing:

// Registered on your connect endpoint — server-side only.
session: async ({ avatarId }) => ({
  instructions,
  transcript: { url: "https://your.app/api/rta/transcript", secret: TRANSCRIPT_SECRET },
  // Echoed verbatim, so the keys are yours — except user_id, which
  // GET /usage/sessions?endUserId= filters on.
  metadata: { user_id: user.id, characterId: avatarId, mode: "video" },
})
import hashlib, hmac, json, time
from fastapi import BackgroundTasks, HTTPException, Request

# Mint half — in your connect route, next to avatar_id. Wire names, not the
# SDK's transcript / metadata.
body["transcript_webhook"] = {"url": "https://your.app/api/rta/transcript",  # https only
                              "secret": TRANSCRIPT_SECRET}                   # 16..200 chars
body["client_metadata"] = {"user_id": str(user.id), "characterId": character.avatar_id,
                           "mode": req.mode}                                 # up to 16 string pairs

# Receiver half.
MAX_SKEW_SECONDS = 300


def verify(body: bytes, signature: str, timestamp: str, secret: str) -> bool:
    if not timestamp.isdecimal() or abs(time.time() - int(timestamp)) > MAX_SKEW_SECONDS:
        return False                                  # missing, malformed, or stale: replay-bound
    signed = f"{timestamp}.{body.decode()}".encode()
    expected = hmac.new(secret.encode(), signed, hashlib.sha256).hexdigest()
    return hmac.compare_digest(f"v1={expected}", signature)


@app.post("/api/rta/transcript")
async def transcript(request: Request, tasks: BackgroundTasks):
    raw = await request.body()                        # RAW bytes, before any parsing
    if not verify(raw, request.headers.get("x-rta-signature", ""),
                  request.headers.get("x-rta-timestamp", ""), TRANSCRIPT_SECRET):
        raise HTTPException(401)
    payload = json.loads(raw)
    if already_saved(payload["session_id"]):          # at-least-once: dedupe on session_id
        return {"ok": True}
    user_id = (payload.get("client_metadata") or {}).get("user_id")   # None when the mint set none
    tasks.add_task(save_turns, user_id, payload["segments"])
    tasks.add_task(save_tool_calls, user_id, payload.get("tool_calls", []))   # absent when none ran
    return {"ok": True}                               # answer inside 5 s; save after

Sign over the raw request bytes. Parsing to a dict and re-serializing changes the whitespace, and the signature will never match. TRANSCRIPT_SECRET is the same secret you passed as transcript_webhook.secret at mint. The URL must be https — the mint accepts http, but the worker silently drops it and nothing is ever delivered.

Registering it without the SDK. The TypeScript tab above turns the webhook on; the Python tab only verifies what arrives. If your server mints over raw HTTP, the field on the mint body is transcript_webhook — not transcript, which is the SDK's name for it — and the mint is strict, so the wrong key is a 422 rather than a dropped field and no webhook is ever sent. secret must be 16–200 characters: it is the HMAC key the handler below verifies against, so generate it rather than typing one.

The POST carries x-rta-signature: v1=<hex hmac> and x-rta-timestamp; the signed payload is "<timestamp>.<body>" under HMAC-SHA256 with your shared secret. Verify it before trusting anything, and reject stale timestamps. client_metadata comes back verbatim ({} when the mint sent none), so you can attribute the transcript without a session lookup, and session_id is the key to dedupe a retry on.

tool_calls is present only when the session ran at least one tool. Each entry is {name, call_id, arguments, ts, ok, result | error, duration_ms}; an entry with no ok field means the call produced nothing the model saw. Store it next to the segments — a reply grounded in a lookup is only auditable if the lookup itself is in the history. Arguments and results are truncated to 2,000 characters each and at most 200 calls are recorded (tool_calls_truncated: true marks an overflow); this is a history, not a replay. One known gap: a tool round belonging to a turn the user barged in over is not reported by the runtime, so its calls are absent even though they ran.

You get the transcript and decide where it lives: register a transcript webhook when your server mints the call, and after it ends the platform POSTs the full two-sided transcript and the tool calls the model made to your HTTPS endpoint, signed with HMAC-SHA256 over the timestamp and body so you can verify it before storing it. Separately, the platform keeps private session logs for diagnostics; they can contain conversation text, are kept while the account exists, and are removed when the account is deleted. Request diagnostics, billing records and model-provider processing follow the retention rules in the privacy policy.

Bringing your own model

Can I use my own LLM or agent? Yes, two ways. Your own model (a custom LLM): point the call at an OpenAI-compatible /chat/completions endpoint you host. Your server mints the session with llm backend "external", the endpoint's https URL and a per-call token, and the platform streams every reply from it while it keeps handling listening, turn-taking, interruption and video. External endpoints are self-serve. The worker refuses a host that resolves to a non-public address and never follows redirects. Your own agent loop, self-serve: your server runs the agent, each result reaches the character through sendTurn with per-turn instructions, and in video calls listen: false turns the platform's speech recognition off so your client supplies every turn's text. Pair either with voice_webhook to use your own voice as well. Not offered: a renderer-only mode that lip-syncs audio streamed from your own voice agent, such as one built on LiveKit Agents, Pipecat or OpenAI Realtime.

An external endpoint gets one streaming POST {base_url}/chat/completions per reply, with your per-call token as Authorization: Bearer. Mint it from your server with llm: { backend: "external" } and external_llm: { base_url, token }, or in the SDK the policy's llm: { backend: "external", baseUrl, token }; a browser-minted request cannot carry it. If your endpoint fails before the first token of a reply, that reply is spoken by the platform's model so the call never goes silent; set fail_closed: true (failClosed in the SDK) to fail the reply instead.

With listen: false (wire stt_mode: "off") in an avatar-mode call, your client supplies every turn's text through sendTurn and the platform's model phrases the reply under your per-turn instructions. For a line that must be exact, use sendClosingTurn, which speaks your text verbatim and then ends the call. Voice-only calls always run the platform's speech recognition.

Compare and cost guides