Core API and events
The adapters (Vercel AI SDK, Anthropic, OpenAI) are thin layers over four methods. Use them directly to add Contextwaise to any agent loop, in TypeScript or Python.
Any framework#
Call shrink when a tool returns, and give the model its text instead of the raw output:
Use the tool call's own id as callId. Asking again for the same id returns the same view, so rebuilding messages on later turns keeps them byte-identical.
Methods#
shrink(input)#
Returns the model's view of a tool result. Decided once per callId, then replayed.
callIdstringrequiredThe tool call's id.
toolstringrequiredThe tool's name. Selects its budget and groups results in the dashboard.
argsunknownrequiredThe call's arguments, used to judge relevance.
outputunknownrequiredThe raw result: a string, or any value (serialized as JSON).
noteIntent(callId, text)#
Records why the agent made a call: the text the model wrote before it, or the user's request. Call it before shrink. Arguments alone rarely say what the agent is looking for.
expand(ref, question?)#
Returns the original output behind a ref, or the parts of it that answer question. Expose it to your model as a tool so it can recover anything that was trimmed. Pass the input size of the model call that asked for it: an expand costs one more model call about that size, and the dashboard counts it against the savings (the adapters do this for you).
flush()#
Sends pending dashboard reports now. Await it before a script or serverless function exits.
The result#
textstringrequiredWhat the model reads.
mode"full" | "shaped" | "trimmed"requiredfull: untouched. shaped: noise removed, fits the budget. trimmed: only the relevant parts kept.
refstringrequiredThe reference to pass to expand.
kindstringrequiredThe detected format: json, html, test, listing, log or text.
tokensBefore · tokensAfternumberrequiredSize of the raw output and of the view.
notesstring[]requiredWhat cleanup did, in plain words.
sectionsarrayrequiredEach part of the output with its score, whether it was kept, and why.
Ledger#
The ledger keeps each original and its decision, so views can be replayed and expanded. The default lives in memory and holds the last 5,000 tool calls, which suits a single process. When an agent's steps can run in different processes, store it somewhere shared:
Events#
onEvent receives every decision as it happens:
| Event | When |
|---|---|
shrink | A result was decided: { callId, tool, result, ms }. |
replay | A known result was asked for again and replayed as is. |
expand | The model asked for a trimmed part: { ref, question, tokens }. |
engine_fallback | The API couldn't judge a result's relevance, so the model got its cleaned output whole. |
api_error | A call to the API failed, with a message. |