Shrink agent tool results the moment they return.
Agents replay every tool result on every later step. The Contextwaise SDK, for TypeScript and Python (contextwaise on npm and PyPI, published at launch), cuts each one once, before the model reads it: one line for the Vercel AI SDK, one call per turn on the Anthropic and OpenAI SDKs.
import { generateText, isStepCount } from "ai";
import { openai } from "@ai-sdk/openai";
import { createContextwaise } from "contextwaise";
import { withContextwaise } from "contextwaise/ai-sdk";
// Reads CONTEXTWAISE_API_KEY and CONTEXTWAISE_URL from the environment.
const cw = createContextwaise({ agent: "code-review" });
const { model, tools } = withContextwaise(cw, {
model: openai("gpt-6.1-sol"),
tools: { runTests, readFile, searchLogs },
});
// Every tool result is cleaned; large ones reach the model
// trimmed to what this step needs.
// The full output stays with you, one expand() call away.
const { text } = await generateText({
model, tools, prompt, stopWhen: isStepCount(10),
});Agents replay every tool result. Shrink it once.
A test log read at step 3 of a 20-step run is billed on every step after it. On our demo agent, a 7,209-token test run reached the model as 156 tokens, every failure kept.
expand with a question to get any omitted part back.Works with the SDK you already use.
Contextwaise plugs in where tool results go back to the model: your tools, your prompts and your provider stay the same, and your app keeps every raw output.
One line wraps your model and tools, through the AI SDK's own toModelOutput hook. generateText and streamText work as before.
withContextwaise(cw, { model, tools })Read the docs You keep your loop. One call per response runs Claude's tool calls and returns the shrunk tool_result blocks to send back.
toolResults(cw, response, run)Read the docs Responses API or Chat Completions: one call per response runs the function calls and returns the outputs to send back.
functionOutputs(cw, response, run)Read the docs Another framework? Call shrink when a tool returns and give the model its view. Adapters for LangChain, the OpenAI Agents SDK, the Claude Agent SDK and MCP are next.
A tool result is paid for on every step after it.
In an agent loop, every model call resends the whole conversation, tool results included. A 20,000-token test log read at step 3 of a 20-step run is billed again on each of the 17 steps that follow. Prompt caching makes those repeats cheaper, not free, and the first read is always at full price.
Most agents need a small part of what their tools return: the two failing tests, not the 336 passing ones; the one folder that matches, not the whole tree. On our demo agent, a 7,209-token test run reached the model as 156 tokens, and a 28,309-token log as 564, with the failures and errors kept.
Cleaned, judged, then sent. Once.
Every tool result goes through the same steps the moment the tool returns. Most results never need the network: cleanup alone brings them under budget.
A tool call already seen gets exactly the same view as before, byte for byte.
Noise is removed by content type: test output, logs, JSON, HTML and file listings (grouped by folder, with nothing lost).
A result that fits its budget passes whole, and so does one a cut would barely shrink (under 1,000 tokens or 30% saved): a small cut isn't worth the risk of an extra model call to expand it. Give file-reading tools a bigger budget so files the agent may edit arrive intact.
Bigger results are cut into sections and scored against the agent's intent by our decision model through the API. Without a key, or if the API fails, the cleaned output is sent whole.
The relevant parts reach the model in their original order. Each gap says which lines or items are missing, and a note counts what was cut (like "status: delivered ×148") and says how to expand it.
What the model reads instead.
A Vercel AI SDK agent on OpenAI GPT-6.1 Sol with six synthetic tools, five questions in one conversation. Tools returned 52.6K tokens; the model read 9.1K. The agent's cost was $0.057 instead of about $0.129 (the comparison replays the same requests with raw outputs through a prompt-cache simulator). Your workload will differ.
| Tool output | Returned | Model read | What happened |
|---|---|---|---|
| Test run | 7,209 tokens | 156 tokens | 336 passing tests and progress bars removed, both failures kept. |
| Application logs | 28,309 tokens | 564 tokens | Repeated lines collapsed, then the relevance model kept the errors, their causes and warnings. |
| File listing | 3,782 tokens | 481 tokens | 453 paths grouped into 28 folder lines. No path lost. |
| Orders API (JSON) | 3,450 tokens | 1,408 tokens | 216 empty fields dropped. Every order kept: a total needs all of them. |
Built for agent loops.
One line wraps a Vercel AI SDK agent, in TypeScript or Python. On the Anthropic and OpenAI SDKs, one call per model response runs your tools and returns the shrunk results, in TypeScript or Python. Your app still gets every raw tool output.
Passing tests, progress bars, repeated log lines, empty JSON fields and HTML markup go, locally, in 1 to 20 ms on our demo agent.
Relevance is judged against what the agent is doing: the user's request and what the model said before calling the tool.
Omitted parts stay in your process. The model calls expand with a question and gets back exactly the part it needs.
Each result's view is decided once and replayed byte for byte by tool call id, so the provider's prompt cache keeps hitting.
Only results still over budget after cleanup are sent for scoring. Usage reports carry sizes and tool names, never tool output.
Questions about agent sdk
Does the model lose information?
No. Omitted parts stay in your process, and every trimmed result tells the model how to get them back: it calls expand with a question and receives exactly the part it needs. Results that fit their budget pass whole, and error sections are kept first.
Is it safe for prompt caching?
Yes. Each result is shrunk once and replayed byte for byte. See prompt caching.
Do I need an API key?
No. Without one, the SDK still cleans tool output locally and free, then sends it whole. With a Contextwaise key, results still over budget after cleanup are scored by our decision model and only what the task needs is kept.
What leaves my machine?
Without a key, nothing. With one, only results still over budget after cleanup, cut into sections for scoring, and usage reports with sizes and tool names. Full outputs never leave your process.
What if the API is slow or down?
The agent never waits on it: scoring times out after 4 seconds and for the next 30 seconds the model reads cleaned outputs whole, nothing cut. Reports are sent in the background and dropped if they cannot be.
Cut your LLM costs
without cutting quality.
One layer between retrieval and inference removes the context your model never needed, in milliseconds. Fewer input tokens, lower cost, a prompt cache that keeps hitting, and every removal logged with its reason.