Context Pruning: Shrink Data Before the Model

Picture an agent that fetches 4 MB of order data from an API, pastes the whole thing into its context and then tries to add up the refunds in its head. It gets a number. The number is wrong. It is also very expensive to be wrong that way. Context pruning is the boring habit that prevents this: shrink the data with ordinary code before the model ever sees it.
What context pruning means
Every token you feed a model costs money and attention. Raw API responses are mostly noise for the question at hand: nested metadata, fields nobody asked about, page after page of rows. Context pruning means filtering, sorting, paging and doing the arithmetic in a sandbox, then handing the model only the small result it needs.
Anthropic's engineering post on code execution with MCP gives a clean example. Fetch a 10,000-row spreadsheet the old way and all 10,000 rows land in context. Let the agent write code that filters in the execution environment, and the model sees five rows. TanStack put the other half of the argument more bluntly in its Code Mode announcement: LLMs are hilariously bad at math, so let a runtime do it.
Why Dynamic Workers fit context pruning
You need somewhere safe to run that model-written code. Cloudflare's answer is Dynamic Workers, in open beta since 24 March 2026 for paid Workers plans. A Worker can create another Worker on the fly with code supplied at runtime, through a LOADER binding:
env.LOADER.load()takes the code as modules, plus the bindings you choose to give it.globalOutbound: nullcuts off internet access, or you can intercept requests instead.- Each one runs in its own V8 isolate, which Cloudflare says starts in a few milliseconds using a few megabytes, roughly 100 times faster than a typical container.
Because isolates are that cheap, you can spin one up for a single snippet and throw it away. That makes the sandbox a natural buffer between messy APIs and the model. Cloudflare's own API server works this way: generated code runs in a Dynamic Worker, and in its OpenAPI helper text responses are capped at roughly 6,000 tokens, with a marker when they're cut. Even a runaway query can't flood the model.
A quick example
Back to that refund disaster. In the pruned version, the agent writes a short async function. It calls the orders API through a binding you provided, pages through results, keeps only orders with status refunded from the last 30 days, sums the amounts in JavaScript, and returns three things: the total, the count and the five biggest refunds. Megabytes go into the isolate. About 200 tokens come out. The addition is done by a CPU, which, unlike that agent, has never been confidently wrong about arithmetic.
I'd add one caution. Pruning code can drop things the model actually needed, and it's easy to filter on the wrong field. My advice: log what the code returned and keep the raw response somewhere you can check later, so a bad filter is a quick fix rather than a mystery.
The bigger picture
This is the same move behind Code Mode: treat the model as the planner and let code do the heavy lifting. Add agent skills for instructions and a credential proxy for secrets, and you get an agent that only carries what it needs.
Here is the price detail that surprised me. When Cloudflare announced the beta, it priced Dynamic Workers at $0.002 per unique Worker loaded per day on top of normal CPU and request costs, and waived even that during the beta. For a one-off pruning job, the sandbox is a rounding error next to the tokens it saves. So how much of your agent's context is data it never needed to read?