Stop Proxying LLM Trace Payloads Through Your API: Presigned URLs vs. CloudFront Signed Cookies
In this piece
The monitoring UI wanted the full prompt. The ClickHouse row carried InputTruncated, a bounded display preview; the complete text lived in S3. So the backend did the obvious thing: it fetched the object, buffered the bytes, and returned them in the HTTP response.
That is the moment an API becomes a byte proxy. Every payload large enough to truncate is large enough to hurt the service that relays it.
That proxy was our first version. We replaced it with presigned URLs: the API stops carrying the bytes and authorizes the browser to fetch them directly. The choice of signing mechanism follows the access pattern:
| Mechanism | Use it when | Status here |
|---|---|---|
| S3 presigned URL | You need one object and can expose the S3 origin | What runs in production today |
| CloudFront signed URL | The origin must stay hidden, or repeat reads should hit an edge cache | Supported in code, not configured in production |
| CloudFront signed cookies | One user action authorizes many related objects | Considered only |
In every case, authorize the tenant and scope the key before you sign.
Why the API should not carry the bytes
A trace view reads two stores. Metadata, timestamps, token counts, and truncated previews come from ClickHouse in one fast query. The full prompt, completion, tool outputs, and variables come from S3, object by object, under keys shaped data/{workspaceId}/{year}/{month}/{day}/.... A single trace with a dozen spans can mean a dozen multi-megabyte objects.
A 12 MB span object fetched by the backend and returned to the browser crosses the application process twice. The costs compound:
- Memory and bandwidth. Every open download holds payload bytes in API memory and uses API network capacity, so the service’s load grows with payload size, not only with traffic.
- No cache. Two users opening the same trace cost two full reads from S3 and two full transfers to the browser. A CDN could serve the second from cache; the proxy cannot.
- Timeout surface. A slow client holds a backend connection open for the whole transfer. The connection pool that serves authentication now shares its capacity with payload downloads, the same coupling the series opener traced into an outage.
Presigned S3 URLs: sign, do not relay
The simplest mechanism is an S3 presigned URL. The backend computes a signature over a GetObject request and a deadline, then returns the URL. The browser fetches the object from S3 directly.
- Browserrequests the payload
- HTTPS
- APIverifies tenant, signs a GET, returns the URL
- short-lived URL, API out of the path
- BrowserGETs the signed URL from S3
- direct
- S3returns the object bytes
The backend authorizes one object with a short-lived signature. The bytes travel directly from S3 to the browser, never through the application.
We already use this primitive on the upload side. createPresignedUploadUrl signs a PutObject with a fifteen-minute expiry and can pin content-length as a signable header.
Presigned URLs are the right default for a single object, with three costs:
- Exposed origin. The browser learns the bucket’s regional endpoint.
- No cache. Every fetch goes to the origin.
- One URL per object. Signing is a local computation, so one API response can return twelve URLs cheaply. The cost moves to the client: twelve URLs to request, track, and refresh when they expire.
CloudFront signed URLs: hide the origin, add the edge
CloudFront serves the S3 origin through its own domain and caches responses at edge locations. With Origin Access Control, CloudFront signs its requests to S3, and the bucket policy grants read access only to that distribution, so direct S3 requests are denied. The backend signs a URL or a policy statement with a private key, and CloudFront checks the signature at the edge against a public key in a trusted key group.
The code already supports this, but production does not configure CloudFront today, so it runs on the S3 fallback. The read path branches on configuration:
// Use CloudFront signed URLs if configured, otherwise fall back to S3 presigned URLs
if (
env.CLOUDFRONT_DOMAIN &&
env.CLOUDFRONT_KEY_PAIR_ID &&
env.CLOUDFRONT_PRIVATE_KEY
) {
return getCloudFrontSignedUrl({
url: url.toString(),
keyPairId: env.CLOUDFRONT_KEY_PAIR_ID,
dateLessThan,
privateKey: env.CLOUDFRONT_PRIVATE_KEY,
});
}
// Fallback to S3 presigned URLsSigned URLs solve the first two problems: the CDN domain hides the origin, and a second reader may be served from an edge cache. The signature is still per-object, so a multi-span trace still needs one URL per payload.
CloudFront signed cookies: one session, many objects
When the read pattern is open one trace, fetch everything in it, per-object signing is the wrong granularity. CloudFront signed cookies fit that pattern.
The backend sends CloudFront-Policy, CloudFront-Signature, and CloudFront-Key-Pair-Id in a single response. The policy scopes access to a URL prefix and an expiry. The browser attaches the cookies to every subsequent request under the CDN domain, and CloudFront validates them at the edge. In practice the CDN needs a hostname under the application’s own domain, such as a cdn. subdomain, so the API can set cookies the browser will send to it.
- Browseropens the trace
- one round-trip
- APIverifies tenant, sets signed cookies for the trace prefix
- sets Policy, Signature, Key-Pair-Id cookies
- BrowserGETs input.json, cookie attached
- cookie attached
- CloudFrontserves input.json, from cache or S3
- no re-signing
- BrowserGETs output.json, same cookie
- same cookie
- CloudFrontserves output.json, from cache or S3
One Set-Cookie response authorizes a scoped prefix. The browser fetches every related object in the trace view without further signing round-trips.
The convenience is what makes them dangerous when the scope is wrong. A cookie scoped to data/{workspaceId}/.../{traceId}/* lets the browser fetch every object under that trace and nothing else. A cookie scoped to data/{workspaceId}/* lets it read an entire workspace’s payloads until the cookie expires.
Signed URLs and signed cookies also do not mix on the same request. If a URL carries any of the Expires, Policy, Signature, or Key-Pair-Id query parameters, CloudFront treats it as a signed URL and ignores cookies. Pick one model per resource path.
We have not built this: signed cookies have only been considered. They are the pattern I would reach for if one trace view grows to many objects and handing out one URL per object becomes the awkward part.
Neither mechanism is authorization
A presigned URL, a CloudFront signed URL, and a signed cookie all answer one question: may this requester fetch these bytes? They do not answer: does this requester own this trace?
Ownership is decided at signing time, on the server, against the tenant and feature the user belongs to. Every span object lives under data/{workspaceId}/..., so the workspace prefix is the tenant boundary.
A signing endpoint that resolves the authenticated user’s workspace and signs only keys under that prefix is doing authorization. An endpoint that signs any key the client hands it is a confused deputy.
It bites harder for cookies because their scope is wider. A signed URL authorizes one object; a signed cookie authorizes every object its policy matches, for as long as it lives. CloudFront validates both at the edge without asking the API, so neither can be revoked one at a time.
The kill switch is the same for both: remove the public key from the trusted key group, and every URL and cookie signed with it stops working. Add the replacement key first, or every reader loses access at once.
A short security checklist
| Concern | What to enforce |
|---|---|
| Expiry (TTL) | Sign for the shortest window the workflow needs. Our upload URLs expire in fifteen minutes; read URLs should be no more generous. |
| Object and prefix scope | Sign the exact key, or for cookies, the narrowest prefix that covers the session. |
| Private origin | Once a CDN is in front, use Origin Access Control and a bucket policy that only admits the distribution. |
| Revocation | Assume a signature stays valid until it expires. Removing its public key from the key group is the only kill switch. |
| Logging | Enable CloudFront and S3 server access logs. A leaked URL should be traceable, and a surprise fetch should be findable. |
The signing endpoint is still the place that decides who may read what.
Filed under
Next in Rebuilding an Ingestion Pipeline
One Terabyte a Day: The Full Ingestion Architecture
Part 10 continues the series.