Budget tokens across a session
Calculate context costs, design a reusable prefix, and manage the lifetime of facts and tool results.
A document-review service repeatedly sends a stable policy, long tool outputs, and a short case-specific request. Context capacity, token spend, and time to first useful output require separate measurements.
2.4 · Calculate a context budget
Inventory the serialized inputs, including system text, tool definitions, examples, history, and retrieved evidence. In an illustrative 32,000-token budget, 11,000 for instructions and tools, 12,000 for history, and 4,000 reserved for the answer leave 5,000 for evidence. A cache hit can reduce processing cost and latency while the same prefix still occupies context. Removing irrelevant 3,000-token tool payloads from 2,000 calls avoids sending six million input tokens.
Keep citations, tool-call relationships, and facts required for the next decision. Check grounding on the longest cases after trimming. Average prompt length can hide failures at the limit.
Recover space without losing the decision
A 32,000-token request reserves 4,000 for the answer and carries 11,000 tokens of stable instructions, 9,000 of history, a 6,000-token tool dump, and a new 4,500-token appendix. The dump contains a current contract ID and a 180-token decision record amid thousands of irrelevant entries. How far over budget is the request, and what should the model receive?
Balance the context ledger
The effective input budget is 28,000. The proposed input totals 30,500, an excess of 2,500. Replace the dump with a verified projection containing the required ID, decision and evidence references. If that projection takes 250 tokens, input falls to 24,750 with 3,250 tokens of input headroom. Preserve outstanding tool-call relationships and the precise source locations needed to answer follow-ups.
Test a follow-up that requires the exception buried in the original payload. If the projection omitted it, provide an authorized lookup by ID. A projection that fits but loses the only supporting exception fails the task.
Video background · freeCodeCamp at 7:16:52 · inspecting the context budget
2.5 · Design cache breakpoints and reusable procedures
Put identical stable content first, choose a supported cache breakpoint after the reusable prefix, and append changing user data afterward. Changes before the breakpoint can invalidate reuse. Measure cache creation, reads, expiry, and the traffic interval. For ten requests sharing an eligible 8,000-token prefix within its lifetime, one initial write and nine reads are possible. Calculate cost with actual write/read rates, and include uncached suffixes. A cold or infrequent workload may see little benefit.
Use versioned modules for common instructions and Skills for reusable procedures loaded when appropriate. Updating a shared module can change both behavior and cache hit rate. Track its revision in evaluations.
Price a warm prefix and a cold one
Use illustrative rates of 1 unit per 1,000 uncached input tokens, 1.25 per 1,000 cache-write tokens and 0.1 per 1,000 cache-read tokens. Four requests each contain the same eligible 8,000-token policy and a unique 500-token suffix. Price the input when all four arrive inside the cache lifetime, then repeat when every call arrives after expiry.
Compare the two traffic patterns
Without caching, four inputs cost 34 units. In the warm burst the first write costs 10, three reads cost 2.4, and four suffixes cost 2. Total input cost is 14.4 units. With four cold writes the total becomes 42. These rates are for the exercise. Use the selected provider’s current rates and actual creation/read usage for a deployment estimate.
Place the stable policy and any identical instructions before the breakpoint. Add each case ID afterwards. A shared Skill can hold the procedure. If its content changes before the breakpoint, measure cache creation and behavioral regressions under the new revision.
2.6 · Set reasoning effort and manage long-lived context
Use context editing to clear obsolete tool results when the task has moved past them. Use compaction to summarize a long interaction while retaining current decisions and constraints. For facts needed across sessions, use an explicitly scoped persistent store or memory tool, with an owner, retention policy, and deletion path. Tool output disappearance and durable deletion are different events.
A short lookup can use low effort. Increase effort on the few tasks whose measured accuracy benefits enough to justify the added time. Before compaction, preserve verified IDs, deadlines, evidence locations, and unresolved questions outside a lossy narrative summary.
Resume a review after compaction
At turn 22 the user accepted clause 8(b) of revision 4 subject to a Friday deadline. A tool returned 200 older clauses. By turn 68 the assistant remembers “clause 8 was accepted.” Meanwhile revision 5 has arrived. Design a resume test that distinguishes durable facts, summarizable discussion, and obsolete output.
Reconstruct the next decision
Persist the customer-scoped case ID, revision 4, exact clause, deadline and supporting event in a record with retention and access rules. Summarize the discussion and clear stale tool output when subsequent turns no longer need it. Resume in a fresh session, reload the scoped record and verify the citation against revision 4. Then present revision 5 and require a fresh comparison before applying the old acceptance.
Use higher reasoning effort for the amendment comparison only if it improves held-out decisions within the deadline. A lookup of the preserved ID can remain inexpensive. An acceptable resume test reports the exact old commitment and flags the new revision as unresolved.
Video background · freeCodeCamp at 10:44:33 · preserving exact facts between turns
Fork a conversation and check the work outside it
An analyst forks a case conversation to compare two proposed replies. Branch B edits a shared case file after the fork. Returning to branch A restores A’s conversation history, but the shared file still contains B’s edit. Session history, workspace state, and committed external actions have different lifetimes.
For a reproducible comparison use separate working copies of a named source revision, read-only tool credentials, and distinct output locations. On resume, compare the current record revision with the branch’s verified checkpoint. If they differ, refresh the evidence before committing and apply the normal approval and idempotency checks.
Further study · freeCodeCamp at 6:47:56 · forking a session from shared history · Claude documentation
Design exercise
A long review loses an exact clause number after compaction. Write a case-facts record with document revision, clause, passage ID, and verification time. Resume with the record and compare the resulting citation against the original. Then test a revised document to ensure the stored reference can be invalidated.
Further reading · Prompt caching · Context editing · Context compaction · Reasoning effort · Memory tool
Budget tokens across a session quiz
Answer 10 questions. Check each answer for an explanation.