Contextwaise
Agent SDK

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.

GitHub
Docs: set up the agent SDK
agent/run.ts
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),
});
Agent SDKVercel AI SDK

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.

Withoutrun_tests read at step 3
step 37,209
step 47,209
step 57,209
step 67,209
step 77,209
step 87,209
tokens read, steps 3 to 843,254
With the SDKrun_tests read at step 3
step 3156
step 4156
step 5156
step 6156
step 7156
step 8156
tokens read, steps 3 to 8936
Shrinks at birth
Each tool result is cut the moment the tool returns, before the model reads it, and stays that size for the rest of the run.
Nothing lost
The full output stays in your process. The model calls expand with a question to get any omitted part back.
Cache-safe
A result's view is decided once and replayed byte for byte, so your provider's prompt cache keeps hitting.
Integrations

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.

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.

Core API
The problem

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.

What happens to each result

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.

01
Replay

A tool call already seen gets exactly the same view as before, byte for byte.

02
Clean

Noise is removed by content type: test output, logs, JSON, HTML and file listings (grouped by folder, with nothing lost).

03
Size gate

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.

04
Judge

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.

05
Send

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.

Measured on our demo agent

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 outputReturnedModel readWhat happened
Test run7,209 tokens156 tokens336 passing tests and progress bars removed, both failures kept.
Application logs28,309 tokens564 tokensRepeated lines collapsed, then the relevance model kept the errors, their causes and warnings.
File listing3,782 tokens481 tokens453 paths grouped into 28 folder lines. No path lost.
Orders API (JSON)3,450 tokens1,408 tokens216 empty fields dropped. Every order kept: a total needs all of them.

Built for agent loops.

Fits your stack

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.

Free cleanup first

Passing tests, progress bars, repeated log lines, empty JSON fields and HTML markup go, locally, in 1 to 20 ms on our demo agent.

Intent-aware

Relevance is judged against what the agent is doing: the user's request and what the model said before calling the tool.

Nothing lost

Omitted parts stay in your process. The model calls expand with a question and gets back exactly the part it needs.

Cache-safe

Each result's view is decided once and replayed byte for byte by tool call id, so the provider's prompt cache keeps hitting.

Your tool output isn't stored

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.

See it live
Keep your model, retriever and prompts. Remove one call to roll back.