---
title: "A proxy that holds the body owns its size"
date: 2026-11-02
summary: "A coding session full of screenshots made every model request 87 MB, and a Worker in the middle buffered each one and answered 503. Moving the key to egress let the body stream, and let the model API's own 413 reach the client that knows what to do with it."
tags: ["proxies", "sandboxes", "secrets", "errors"]
url: https://blog.misherate.com/posts/a-proxy-that-holds-the-body-owns-its-size
thumbnail: https://blog.misherate.com/thumbnails/a-proxy-that-holds-the-body-owns-its-size-mwfgx507.jpg
---

Misha's [iterate](https://iterate.com) mind runs agents in sandboxes without ever handing them a real API key: keys are filled in on the way out.

Eighty-three screenshots made every model request 87 MB, and a well-meaning proxy in the middle answered each one with a 503.

One of the agents the mind runs is [opencode](https://opencode.ai), on a sandbox of the mind. It reaches models through the mind, not with keys of its own: no real API key is ever on the sandbox. For Claude, that used to mean a route on a small bridge, a Worker the sandbox calls with a bridge secret. The bridge built a fresh request with an allowlist of headers, put a key placeholder in it, and sent it on through the mind's egress, which swaps the placeholder for the real key on the way to the one origin it is pinned to.

It was careful about headers. It was not careful about size.

## What broke

On 1 November a session took 83 full-size screenshots. Model APIs keep no memory, so every step of an agent re-sends the whole conversation, and every image in it goes again as base64 text. Each request reached 87 MB. The bridge read each one into memory before forwarding it:

```ts
// the old route, simplified
async function forward(request: Request) {
  const body = await request.text(); // the whole request, in a Worker's memory
  const headers = new Headers({
    "content-type": "application/json",
    "x-api-key": 'getSecret("/secrets/model-api")', // egress fills this in
  });
  return fetch("https://model-api.example/v1/messages", { method: "POST", headers, body });
}
```

The Worker answered 503, "Worker exceeded memory limit". Two things made that worse than a failed request.

First, a 503 looks retryable, so the client retried. The model API has its own answer for a request that is too big, a 413, and opencode treats that one specially: it compacts the conversation and strips media. Our 503 hid that answer, so the client never did the one thing that would have fixed it. Its compaction counts tokens, and images look cheap in tokens, so nothing else was going to trim them.

Second, the [web UI for those sessions](/posts/a-retry-is-a-new-step-so-it-needs-a-new-name) had a rule: any page that couldn't reach the server started it again. A tab left open on the frozen server made 710 of 1,047 attempts to restart it, and those restarts kept it from coming back.

## The fix: let egress do the one thing the bridge was for

The bridge existed so the key never reached the sandbox. But the sandbox's egress already does that by itself: a placeholder in a header is replaced on the way out, for the origin the secret is pinned to. And egress only touches headers; the body streams through. So Claude now goes straight from the sandbox to the API, with the placeholder as its key:

```ts
// opencode's provider config, simplified
provider: {
  anthropic: {
    options: { apiKey: 'getSecret("/secrets/model-api")' }, // egress substitutes it
  },
},
```

No real key on the sandbox, as before, and no Worker holding the request. Tried live, a 90 MB request went through in 12 seconds. The bridge's route for this provider is gone, along with its tests.

The bigger gain is that the API's own errors now reach the client unchanged. A request that is still too big gets the 413 it deserves, and opencode does what it was built to do with one.

## Make the request smaller too

Streaming makes big requests possible; it doesn't make them a good idea. opencode has its own image limits, so the config sets them: above 1,568 px a side or 300 KB as base64, `read` shrinks an image (to JPEG if it has to) before the model sees it. Every image is kept, smaller.

That took two tries. The first set the limits under `attachments`. Tried live, an image came back at 981 KB: the defaults were still in force. This config is written in version 1 of opencode's config shape, where the key is `attachment`; `attachments` is version 2's, and version 1's decoder drops an unknown key without a word.

```diff
-  attachments: { image: { max_width: 1568, max_height: 1568, max_base64_bytes: 300_000 } },
+  attachment:  { image: { max_width: 1568, max_height: 1568, max_base64_bytes: 300_000 } },
```

The unit tests had passed with the wrong key, because they checked the config the code wrote, not what opencode did with it. Only reading an image on the real server showed the setting was ignored.

## Pages start, they don't restart

The UI's rule changed too. A page's request now goes to the server's port without starting anything. If that fails, it asks the sandbox what state it is in, which starts nothing either. A parked sandbox is started, as before; one that is up and not answering gets a 502 with the real error, and no restart.

```ts
async function pageFetch(request: Request) {
  const retry = request.clone();
  try {
    return await portFetch(request);
  } catch (error) {
    if (await sandboxIsRunning()) return new Response(`server did not answer: ${error}`, { status: 502 });
    await startServer();
    return await portFetch(retry);
  }
}
```

The agents' own path still restarts a server that doesn't answer, because a session needs one. A browser tab, polling with nobody watching, doesn't get to make that call hundreds of times.

## What to take from it

- **A proxy that reads the body is responsible for its size.** If all it adds is a header, let something that streams add it.
- **Don't translate errors you can't improve.** A 503 in place of a 413 told the client to try again, when the real answer told it to shrink.
- **A config key that is silently dropped is a setting you don't have.** Check the effect, not the file.
- **Retries from things nobody is watching need a ceiling, or no power to restart at all.**
