Skip to content
tokz

Tool-output compression

Compress tool output before it becomes your agent's next prompt.

Tool output becomes model context: a test report, a cloud API response, terminal logs, or an MCP result. Tokz selects relevant source spans before the next model call, giving an agent less noise without substituting a generated summary.

tool-loop.ts
import OpenAI from "openai";
import { withTokz } from "@tokz/openai";

const openai = withTokz(new OpenAI(), {
  apiKey: process.env.TOKZ_API_KEY!,
});

await openai.chat.completions.create({
  model: "gpt-4o",
  messages: [
    { role: "user", content: "Which deployment failed?" },
    { role: "tool", tool_call_id: "call_1", content: deploymentJson },
  ],
});

The useful part of a tool result is rarely at one position

A failure identifier may appear at the top, its error detail in the middle, and the retryable resource at the end. A selector can preserve non-contiguous material; head or middle truncation cannot make that choice.

Keep the envelope your agent relies on

Tokz integrations preserve tool-call identity, error status, non-text content blocks, and result envelopes. Structured content is only changed where a caller provides a validator and the transformed copy passes it.

Use explicit intent when the application knows it

A user question, tool name, description, and arguments can bound selection. Explicit queries take priority over derived intent, which makes the policy easier to inspect and test.

Limitations

Compression is not a prompt-injection defense and does not make untrusted data safe. Keep security controls and required-field preservation separate from the token-reduction decision.