Skip to content

Twilic AI

Record, replay, and inspect AI streams and agent runs as Twilic binary sessions.

Record once. Replay anywhere. Send less.

Twilic AI turns streaming LLM and agent traffic into compact, replayable .twai sessions. Use it to shrink traces for storage and transport, compare runs, and debug with the same CLI tooling as the rest of Twilic.

Source: github.com/twilic/ai

Packages

PackageRoleDocs
@twilic/aiCore recorder, .twai codec, replay, inspect, diffCore
@twilic/ai-openaiOpenAI Responses API event normalizationOpenAI
@twilic/ai-sdkVercel AI SDK transport + recorder helpersAI SDK
@twilic/ai-agentsAgents SDK tracing processorAgents

Install

bash
pnpm add @twilic/ai @twilic/core

Optional adapters:

bash
pnpm add @twilic/ai-openai @twilic/ai-sdk @twilic/ai-agents

For CLI session tooling:

bash
pnpm add -g @twilic/cli @twilic/core @twilic/ai

Quick start

ts
import { createAIRecorder, ensureTwilicInit, writeSession } from "@twilic/ai";

await ensureTwilicInit();

const recorder = createAIRecorder({
sessionId: "demo-session",
meta: { provider: "openai", model: "gpt-4.1" },
});

await recorder.record(async () => {
recorder.append({
type: "text.delta",
sequence: recorder.events.length,
timestamp: Date.now(),
sessionId: "demo-session",
data: { delta: "Hello from Twilic AI." },
});
});

await writeSession("session.twai", {
meta: {
format: "twai",
version: 1,
sessionId: "demo-session",
createdAt: Date.now(),
eventCount: recorder.events.length,
},
events: recorder.events,
});

Inspect and replay with the CLI:

bash
twilic ai inspect session.twai
twilic ai replay session.twai --speed 10
twilic ai record --input events.jsonl -o session.twai

.twai sessions

.twai is the canonical container for Twilic AI sessions. It stores Twilic-encoded session metadata and a ordered list of canonical AI events.

FieldDescription
MIME typeapplication/vnd.twilic.ai+twai
Extension.twai
MagicASCII TWAI

Each event includes type, sequence, timestamp, sessionId, optional correlation IDs (responseId, itemId, toolCallId), and a data payload. Canonical types include text.delta, reasoning.delta, tool.started, tool.output, response.completed, and more.

See the .twai format page for the on-disk layout.

When to use it

GoalApproach
Persist a chat or agent runRecord events → write .twai
Shrink traces vs JSON/JSONLEncode with @twilic/ai; compare with twilic ai benchmark
Normalize provider streams@twilic/ai-openai for OpenAI Responses
Capture Vercel AI SDK chatsTwilicChatTransport from @twilic/ai-sdk
Trace Agents SDK runsTwilicTracingProcessor from @twilic/ai-agents
Diff two runsdiffSessions or twilic ai diff

Released under the CC-BY-4.0 License.