.twai format
.twai is the canonical on-disk and wire container for Twilic AI sessions.
Record once. Replay anywhere. Send less.
MIME type: application/vnd.twilic.ai+twai
Normative source: spec/twai.md in the twilic/ai repository
File layout
| Offset | Size | Field | Description |
|---|---|---|---|
| 0 | 4 | magic | ASCII TWAI |
| 4 | 2 | version | u16 little-endian (currently 1) |
| 6 | 2 | flags | u16 little-endian, reserved (0) |
| 8 | 4 | headerLen | u32 LE length of header blob |
| 12 | headerLen | header | Twilic-encoded SessionMeta |
| 12+headerLen | 4 | bodyLen | u32 LE length of body blob |
| 16+headerLen | bodyLen | body | Twilic-encoded { events: AIEvent[] } |
SessionMeta
json
{
"format": "twai",
"version": 1,
"sessionId": "uuid",
"createdAt": 1700000000000,
"completedAt": 1700000007000,
"provider": "openai",
"model": "gpt-4.1",
"eventCount": 42,
"source": "recorder"
}AIEvent
Each event is a JSON-compatible object with:
type— e.g.session.start,text.delta,tool.outputsequence— monotonic index within the sessiontimestamp— epoch millisecondssessionId— owning session- optional correlation:
responseId,itemId,toolCallId,model,provider data— payload (text, args, usage, etc.)extensions— provider-specific metadata
Encoding requirements
- Call
init()/ensureTwilicInit()from@twilic/core/@twilic/aibefore encode or decode. - Encode header and body separately with stateless Twilic encode.
- Preserve event order in the body array.
Reference implementation
@twilic/ai provides encodeTwai, decodeTwai, readSession, and writeSession. See Core.