#1035·morphic

PDF attachment token estimate is far below the provider's, so cold-start guards never fire on large PDFs

Author: miurlaCreated Sep 21, 2026Updated Sep 23, 2026
Labelsautomated

Problem

The PDF attachment token estimate is far lower than what the provider actually charges for the same file, so a thread carrying a large PDF slips past every guard that is supposed to bound a cold-start request. The thread then replays the whole document on each cold start, which is exactly the case trimColdStartHistory exists to bound.

The gap is not a rounding error and it grows with the size of the document: the larger the PDF, the further the estimate falls below the real weight, and it errs in the direction that keeps the budget check from ever tripping.

Evidence

lib/utils/attachment-tokens.ts

typescript
export const PDF_BYTES_PER_TOKEN = 30

estimateAttachmentTokens() divides the compressed byte size by that constant. The result feeds three consumers:

  • lib/streaming/helpers/trim-cold-start-history.ts (the COLD_START_HISTORY_TOKEN_LIMIT budget, default 200000)
  • lib/streaming/helpers/cap-historical-attachments.ts
  • lib/streaming/helpers/summarize-carried-context.ts (the attachmentTokens field on the turn's root span)

Bytes are a poor proxy here because a provider bills a PDF by its rendered pages, not by its compressed container, and the compression ratio of a large multi-page document is nothing like that of a small one. The constant was last calibrated in #1025, which moved it in the right direction but kept the single-coefficient model.

How to reproduce the measurement without production data: attach a large multi-page PDF, read attachmentTokens off the turn's root span (that value is exactly ceil(size / PDF_BYTES_PER_TOKEN)), and compare it with the provider-reported input token count for the turn where the PDF first lands. The difference between that turn's input count and the preceding turn's is the document's real token weight.

Proposed direction

Two parts, and the second is the reason this is an issue rather than a patch.

  1. Make the estimate track the real cost better than a single bytes-per-token constant can. Options worth weighing: a size-banded coefficient (the current value is calibrated for small documents and does not hold for multi-megabyte containers), or a page-count signal taken at upload time rather than a byte heuristic.

  2. Decide what should happen once the estimate is correct, because raising it changes behaviour in a user-visible way. A long thread whose subject is the attached PDF would newly exceed the cold-start budget, and trimColdStartHistory drops whole turns, so the document can disappear from a conversation that is entirely about it. capHistoricalAttachments will not catch it first when the thread carries a single attachment, because of MIN_WEIGHT_BLOCK_ATTACHMENTS. So the correction needs one of:

    • an attachment-aware exception in the cold-start trim (keep the turn that carries the attachment, drop its neighbours), or
    • a placeholder path so a dropped attachment leaves a reference behind instead of vanishing, or
    • lowering the attachment weight cap so the attachment guard, not the history trim, is the one that reacts.

Correcting the coefficient on its own is not safe: it trades a cost leak for a correctness regression.

Acceptance criteria

  • The estimate for a large multi-page PDF is within the same order of magnitude as the provider-reported token weight for the turn that introduces it, measured as described above. Small PDFs keep an estimate no worse than today.
  • A cold start on a thread carrying a large PDF is bounded by the cold-start budget rather than replaying the whole document.
  • A thread whose subject is the attached document does not lose the document from its replayed history. Whichever of the three options above is chosen, there is a test that pins this.
  • No change to the guest path and no schema change.