Tool-call codecs
A ToolCallCodec is the strategy that bridges tool semantics and whatever protocol your model actually speaks: it encodes tool specs into the outgoing ModelRequest and decodes the assistant's reply back into { text, calls }. The codec is what makes the kernel provider-agnostic in both directions — the same kernel drives a frontier API with native function calling or a local 7B model with prompt engineering, and swapping protocols is a one-line change.
Choosing a codec
Pass a codec to createAgent via the codec option:
The SDK and the strict local assembly pick a codec for you (native when the provider declares tool support, otherwise JSON). Choose explicitly only when you build on @lite-agent/core directly or want the ReAct protocol.
How the prompt codecs work
Both prompt codecs share the same mechanics:
- Protocol in the system prompt.
encodeappends protocol instructions (including the tool catalog) tosystemand rewrites conversation history into the codec's textual format — assistant tool calls become{"type":"tool_calls",…}JSON orAction:/Action Input:lines, and tool results come back astool_resultsJSON orObservation:lines. - Buffered streaming. Prompt codecs declare
streaming: "buffer": the kernel holds model output until it decodes cleanly, so protocol text never leaks into your event stream astext_delta. - Repair instead of failure. Malformed output throws
CodecErroron decode; the kernel then appends the codec'srepairPromptand asks the model to fix its own output, retrying up tomaxDecodeRetriestimes (default 2) before failing the run. - Deterministic call ids. When the model doesn't supply one, tool-call ids are derived from the response content, so retries and replays stay stable.
Both factories accept a single option, instructions?: string, to append your own protocol guidance after the built-in instructions:
Writing a custom codec
Your fine-tuned local model speaks a bespoke <<tool:...>> syntax? Implement the ToolCallCodec interface and plug it in — tools, checkpoints, and middleware work unchanged:
See also
- The nine strategies — where
ToolCallCodecsits among the kernel's strategy interfaces. - Model providers —
anthropic()/openai()for use withnativeCodec(). - Strict local assembly — automatic codec selection (
"auto") for local runtimes. - Testing utilities — exercise your codec end to end with
fakeProvider.