Pick your brain source
Every avatar reply works the same way end-to-end: your server-side code sends text to POST /v1/sessions/{id}/speak and 2wai synthesizes voice, lip-syncs the avatar, and streams it over LiveKit. The question is where the text comes from. The toolkit supports three answers.
The three options
Section titled “The three options”| Option | Who writes the reply | Where the key lives | Metering | Who it’s for |
|---|---|---|---|---|
| 1. BYO brain | Your own LLM | Your LLM provider’s key, your server | You pay your LLM provider | Everyone, default. Most control, no extra 2wai cost. |
| 2. 2wai brain direct | https://prod.brain.2wai.ai | Partner brn_live_... key, your server | Not metered (pilot) | Named pilots where you want 2wai’s persona-tuned brain |
| 3. 2wai brain via gateway | 2wai gateway → brain | Your sk_ key, your server | Metered on your sk_ | Future — when 2wai sells the brain as a product |
Picking one
Section titled “Picking one”- Use Option 1 (BYO) when you already run an LLM in production, or you want full control over context, memory, retrieval, and tool use. This is the recommended default and what every
apps/avatar_only_*sample in the toolkit uses. - Use Option 2 (direct) when you want the exact persona 2wai hosts for a specific avatar — Dr. Alman’s clinical warmth, Jesus’s theological-honesty guardrails, Mila’s banter. The toolkit’s
apps/internal/enlightenandapps/mint_proxy_examplesshow this shape. - Use Option 3 (gateway, future) when you want 2wai’s brain but prefer per-call metering under one
sk_instead of managing a separate brain key.
All three use the exact same client-side shape — a TwaiBrainCallback / TwaiStreamingBrainCallback in the SDK, or just a POST /v1/sessions/{id}/speak after you’ve got text. The decision is purely server-side.
Option 1 — BYO brain
Section titled “Option 1 — BYO brain”Your server owns the LLM. 2wai never sees the user turn.
async function reply(sessionId, userMessage) { const completion = await openai.chat.completions.create({ model: 'gpt-4o-mini', messages: [{ role: 'user', content: userMessage }], }); const text = completion.choices[0].message.content;
await fetch(`https://api.2wai.ai/v1/sessions/${sessionId}/speak`, { method: 'POST', headers: { Authorization: `Bearer ${process.env.TWAI_SECRET_KEY}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ text }), });}Deep dive: Bring your own brain covers streaming per-sentence, interruption, and the SDK side.
Mint-proxy config:
BRAIN_MODE=none(the default). /brain and /brain/stream return a 410 pointing readers here.
Option 2 — 2wai brain direct, with a partner key
Section titled “Option 2 — 2wai brain direct, with a partner key”Your server calls https://prod.brain.2wai.ai/chat/stream with an X-API-Key partner key issued from the 2wai Avatar Studio. The brain returns an SSE stream of event: delta frames; your server either streams them straight back to the client or splits per sentence and triggers per-clause /speak.
Issue the key
Section titled “Issue the key”Go to 2wai.ai/developers/brain-keys, click Issue brain key, pick live or test, add a label like “Lumara prod”. The full brn_live_... secret shows once — copy it immediately into your server’s .env (or your secret manager). After the modal closes, no path in 2wai can retrieve the cleartext key again.
Rate-limited at 5 keys per 24 hours (shared with your sk_ bucket).
Server-side call
Section titled “Server-side call”const upstream = await fetch('https://prod.brain.2wai.ai/chat/stream', { method: 'POST', headers: { 'Content-Type': 'application/json', Accept: 'text/event-stream', 'X-API-Key': process.env.BRAIN_API_KEY, // brn_live_... }, body: JSON.stringify({ message: userText, user_id: '...', avatar: 'brian-alman', // the brain-side avatar slug model: 'gpt-4o', provider: 'openai', }),});The brain’s response is SSE with event: delta + event: final frames. Pipe it to your client, or process it server-side and call /speak per clause for lower time-to-first-word.
Mint-proxy config
Section titled “Mint-proxy config”Running the toolkit’s reference mint-proxy (apps/mint_proxy_examples/node):
BRAIN_MODE=directBRAIN_API_KEY=brn_live_...With that, your app can POST to /brain/stream on the proxy and get the brain’s SSE back verbatim — the proxy handles the key, HTTPS, and the proper headers.
Rotating + revoking
Section titled “Rotating + revoking”Return to /developers/brain-keys. Click Revoke on the row. The brain’s per-worker negative cache window is 15 seconds, so a revoked key stops accepting traffic within that window even without a brain redeploy.
Issue a new key BEFORE revoking the old one if you want a seamless rotation — have both keys live for the handful of seconds it takes you to swap your server’s env var.
Option 3 — 2wai brain via gateway (future, metered)
Section titled “Option 3 — 2wai brain via gateway (future, metered)”Not yet shipped. When it lands, your mint-proxy will forward /brain/stream to UPSTREAM/v1/brain/chat under your sk_ and the usage will meter on your 2wai developer account the same way /v1/sessions does today.
The client-side shape is identical to Option 2, so a Flutter / Web / iOS app written against Option 2 switches to Option 3 by changing one env var on the mint-proxy:
BRAIN_MODE=gateway# (your existing SK_KEY is reused; no new key needed)Until it ships, the mint-proxy returns 501 on /brain* in gateway mode. Reach out to hello@2wai.ai if you need a metered 2wai-brain route today — we’ll prioritize it with a named customer.
Mixing modes
Section titled “Mixing modes”You can run one mint-proxy per mode if different apps need different brains:
- Your consumer app → mint-proxy A with
BRAIN_MODE=none(BYO) - Your pilot pilot → mint-proxy B with
BRAIN_MODE=direct+BRAIN_API_KEY=brn_live_pilot_...
Both proxies hold the same SK_KEY (same tenant, same quota). Only the brain lane differs.
Related
Section titled “Related”- Bring your own brain — full BYO shape with streaming + interrupts
- Sessions — the
POST /v1/sessions/{id}/speakendpoint every mode ends at - Latency — flushing per-sentence, time-to-first-word
- Mint proxy — Node reference — the three-mode reference server