ByteCosts
proxy-vs-hook-vs-mcp-token-optimization.mdx

AI Coding Agents

Proxy vs Hook vs MCP Token Optimization for Coding Agents

A proxy can shorten eligible content on the provider request path, a hook can change or redirect tools at the agent boundary, and an MCP server can return compact results for tools the agent actually calls. Choose the interception point that sees most of your avoidable input, then verify the client’s auth, stream completion, recovery behavior, and prompt cache.

Map the route from tool call to model request

The agent decides to invoke a tool, the host executes it, and the host assembles another model request with the result. A hook can intervene around execution. An MCP tool can control its own execution and response. A model API proxy can see eligible payloads after the host has assembled the request. Each choice changes where you can observe a reduction and where you need to inspect a failure.

Interception pointWhat it seesWhat to verify before relying on it
Model API proxyEligible messages in requests routed through the proxyActual upstream route, account authentication, stream completion, and cache usage
Host hookThe tool call or result exposed by the host eventMatcher coverage, transformed arguments or output, and failure and cancellation behavior
MCP tool or gatewayCalls and responses for tools registered through it and chosen by the host or modelTool discovery, observed adoption, available recovery tools, and schema overhead

A project can implement several of these paths. Headroom describes a proxy, a library, and MCP tools. lean-ctx describes a CLI, MCP, hooks, and supported proxy paths. sqz documents hooks for a host as well as a gateway mechanism for tool server results. Record which path you actually enable. Installing a plugin or seeing its tools in the client establishes little about how much of a task’s billed text passes through it. Headroom · lean-ctx · sqz

Use a proxy when the expensive content is already in the request

A model API proxy can operate on output from several tool families without requiring the model to select new tools for each action. Headroom’s README describes routing eligible JSON, code, and prose through different compressors. Its live-zone strategy works on new content while keeping the frozen prefix stable. Tamp documents a pipeline with transformations such as duplicate replacement and whitespace reduction, plus replay of older transformed tool results to preserve the prefix. Both mechanisms depend on the request reaching the intended proxy path. Headroom · Tamp

Start a trial by sending a simple request through the client and confirming the intended upstream, model, and account. Then send a representative request with a large result and confirm the proxy changed an eligible payload. A pass-through request can be a legitimate outcome. For example, Tamp’s README describes a request body limit above which larger bodies pass through uncompressed. Check which requests the feature considered and why it skipped them.

Authentication is part of the integration. Tamp’s README documents OAuth routing for Codex and separately warns that changing the base URL in a Kimi Code OAuth setup can affect lookup of the stored token. Headroom’s captured issue list includes an open report whose title describes Claude Desktop overriding a base URL and preventing its wrapper from engaging. Verify the installed client and login type, especially when users expect to keep a subscription flow. For a test requiring credentials, capture routing and status information without logging credential values. Tamp authentication notes · Headroom desktop routing issue

Test more than a request that only emits text. Use an ordinary diagnostic that streams output and issues multiple tool calls, and check that the client sees terminal completion and executes the expected tools once each. Check a normal abort and a recoverable upstream error. If a disconnected stream leads to another billed request, include that request in the cost comparison. An open Headroom issue title reports an SSE disconnection before response.completed on multi-tool runs. The title gives you a scenario to include in a trial. Headroom streaming issue

Use a hook when a host event exposes the relevant work

An RTK hook can rewrite supported shell commands to compact noisy outputs. The agent gets the result at the usual tool boundary, so you can focus a trial on tests, builds, or diffs. Start by checking arguments, exit status, and a failure message with enough detail to diagnose the problem. For rules-file clients, also check whether the model actually chooses the designated RTK invocation. RTK

A hook’s position changes what it can do. A pre-execution event can rewrite or redirect a call before the host runs it. A post-execution event can work on the observed result where the host exposes it. sqz documents both for Claude Code. Its README distinguishes shell constructs it leaves as written at the pre-execution step and handles through post-execution compression. It also distinguishes the latter path from paths that can return a deduplication reference. Read the host-specific path when testing recovery and repeated results. sqz hook behavior

A host update can change accepted hook fields, matchers, permission checks, or which events run for a subagent. Keep a short set of trials you can repeat after an upgrade, including a normal command, a command with quoting or pipes, a failed command, and a cancellation. Use the actual agent execution path so the test includes its event handling. The lean-ctx issue list includes a closed report whose title names an unsupported Codex hook field. The title is evidence that compatibility was reported as an issue, and the current behavior should be checked against the installed adapter. lean-ctx Codex hook report

Where a hook filters new results before they enter the transcript, compare subsequent requests to see whether the transformed text stays stable. A reduction in tool output can still go with a rise in total billed input if the agent reruns commands or if the setup adds instructions to every turn. Count the tool calls the hook actually touches and compare that share with the full agent run.

Use MCP when the tool can return a small answer at the source

An MCP server can expose a specialized tool whose response is already compact. context-mode describes using local execution and SQLite FTS5 indexing to keep large output available for search while returning a selected result. lean-ctx exposes context tools through its engine. A gateway can also transform the results of another MCP server before they reach the client. The sqz README describes a gateway path with reference-based deduplication, plus a variant that avoids references and adds no expand tool. These paths change response behavior for the tools they actually serve. context-mode · lean-ctx · sqz

Check discovery, then count actual calls. An alternate file reader can coexist with the native reader, and the model may still request the full source through the native route. context-mode combines hooks with MCP so selected large operations can be steered toward the context tools. In a combined setup, verify the target tool is available everywhere the redirect applies. Test a normal result, a result that requires the original, and a session where the recovery tool is unavailable. Reversible context compression

Price the tool definitions and any steering text the host includes in model requests. If a tool is rarely used, its definition can add context on turns that get none of its benefit. On repeat turns, check whether the stable tool list reuses the same cached prefix. A gateway that shortens tool responses might leave the schema list unchanged. A gateway that changes tool names, descriptions, or arguments needs a separate test of tool selection and execution as well as size. MCP tool schema overhead

Follow a failed build through each option

Consider a coding task where a compiled target fails and a long build log contains a warning near the actual error. With a hook, the command can run through a filter that returns the failure and a short output. Inspect the returned status and ask whether the model can see the warning or recover the omitted lines. If the agent reruns the build, record the tool time and the model requests that follow. A rerun of a command that can change workspace state deserves the same acceptance check as the first run.

With an MCP path, a local execution tool can write the full log to an index and return only a status and selected matches. Ask for the failing target and then query for the adjacent warning. Watch how many model requests it takes to choose the search tool, retrieve the context, and continue. Check whether the client includes both the old shell tool and the new execution tool in every request.

With a proxy, the host can still observe the full tool response while the provider receives compressed content. Compare what the host stores with the effective provider request. If the next request resends the same history and the proxy rewrites it again, check whether the transformed prefix changed. Read and write counts help distinguish a cache miss from a smaller cache hit. A stream that ends early gives you a separate problem to diagnose, even if the outbound compression is correct.

Run the same task after a client update. Restore the original workspace and repeat the routing, matcher, and discovery checks for the affected path. Then compare the billed categories and task outcome with the earlier trial. That gives you an upgrade check for the configuration you use, including failures that occur before a dashboard has anything to count.

Verify the configuration at the billing boundary

For each setup, capture a task trace from the tool event through the billed requests. Mark whether the change was applied before execution, in the response, or at the provider request. Log where the full output is available and how the agent gets it back. If you test more than one layer, enable each independently before combining them. That isolates a redirect or recovery failure and lets you identify when a second tool is processing a result that the first already compacted. RTK, Headroom, and context-mode comparison

Use a representative task with an acceptance check that depends on exact evidence. Compare fresh input, cache reads, cache writes, output, and turns against the baseline. Also record elapsed time, because an optimizer can shift work to local execution or add model calls. Check what happens when the integration is removed so subsequent trials start from the intended route and tool set. Measure coding agent token savings

A hook is a focused trial when most avoidable input comes from eligible shell commands. An MCP tool is a good fit when the agent repeatedly needs a few relevant passages from large results and can reliably choose the specialized path. A model API proxy has broader visibility in a client that can reliably route the relevant requests through it. If the provider price depends on cached input, treat a stable prefix as part of the acceptance criteria for any of these choices. Context compression versus prompt caching

Sources

How this note was produced

Drafted with AI assistance from committed ByteCosts pricing snapshots and deterministic cost formulas. Figures are grounded in the linked sources (last updated 2026-11-30). Read our editorial & automation policy, inspect the methodology, or report a discrepancy.

Proxy vs Hook vs MCP Token Optimization for Coding Agents. ByteCosts. Updated 2026-11-30. https://bytecosts.com/blog/proxy-vs-hook-vs-mcp-token-optimization/

Sources