Skip to content

Context economy: prompt caching, reachable compaction, bounded outputs (0.6.0) - #88

Merged
fazpu merged 1 commit into
mainfrom
context-economy
Jul 19, 2026
Merged

Context economy: prompt caching, reachable compaction, bounded outputs (0.6.0)#88
fazpu merged 1 commit into
mainfrom
context-economy

Conversation

@fazpu

@fazpu fazpu commented Jul 18, 2026

Copy link
Copy Markdown
Member

Summary

Coordinator context growth is the dominant cost in long harness runs: the whole conversation is re-sent every turn, uncached, and compaction could never fire in SDK runs (threshold ≈ model limit − 33k vs ~96k peaks, user-boundary-only gate). Measured in a real loopy-loop program: ~28M prompt tokens vs 185k completion tokens over 26 iterations.

Changes (all knob-gated, defaults preserve current behavior except the read ceiling):

  • prompt_cache="auto": content-part cache_control breakpoints (system + latest message boundary) for Anthropic-family coordinator models; deterministic tool-schema ordering so OpenAI-family automatic caching gets a stable prefix; cached-token counts pass through into TurnRecord.usage (additive cached_prompt_tokens).
  • compact_above_tokens: absolute-token compaction trigger, firing at user or tool-result boundaries. Initial task pinned verbatim across (repeated) compactions; a trailing tool result is folded into the summary so the rebuilt message sequence stays valid.
  • Bounded agent output: read_agent_output tail clamped to read_output_max_tail_bytes (16 KB) with a banner naming the full logs; read_new_agent_output cap configurable; the spawn footer now asks workers to end with a ≤15-line result card + file paths.
  • Bounded run.json: persisted tool results truncated to run_log_tool_result_max_bytes (8 KB) with a note; live in-memory messages untouched; usage keys unchanged for downstream metering.
  • compact_above_tokens / prompt_cache are TeamHarness ctor params so SDK callers (loopy-loop's signature-introspecting passthrough) can set them per run.

Design doc: loopy-loop design/designs/context-and-eval-economy.md §A1–A4.

Test plan

  • Full suite: 581 passed (+27 new); ruff check/format clean; pyright 0 errors.
  • New coverage: cache injection present for claude / absent for gpt / off-switch; cached-token passthrough; compaction at tool boundary + initial-task preservation + tool-tail fold; tail clamp + banner; result-card footer; run.json truncation with live messages intact; ctor params reach the loop and client; schema ordering determinism.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EPYwF6cRYs2mfsfHF1hibc

Coordinator runs re-send the whole growing conversation every turn; in a
real 26-iteration loopy-loop program that produced ~28M prompt tokens
against 185k completion tokens. Four levers, all knob-gated:

- prompt_cache="auto": cache_control breakpoints (system + latest
  boundary) for Anthropic-family coordinator models, deterministic tool
  schema ordering for automatic OpenAI-side caching, and cached-token
  counts surfaced in usage records.
- compact_above_tokens: an absolute-token compaction trigger that fires
  at user OR tool-result boundaries, so SDK runs can compact mid-run
  (the near-limit rule alone was unreachable: ~967k threshold vs ~96k
  peaks). The initial task is pinned verbatim across compactions and a
  trailing tool result is folded into the summary so the rebuilt history
  stays valid.
- read_agent_output tail_bytes clamped to read_output_max_tail_bytes
  (16 KB) with a banner naming the full logs; read_new_agent_output cap
  now configurable; spawn footer asks workers for a <=15-line result
  card with paths instead of report dumps.
- run.json persists tool results bounded to run_log_tool_result_max_bytes
  (8 KB) with a truncation note; the live message list is never touched.

compact_above_tokens and prompt_cache are also TeamHarness ctor params
so SDK callers (loopy-loop's signature-introspecting passthrough) can
set them per run.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EPYwF6cRYs2mfsfHF1hibc
@fazpu
fazpu merged commit 7358aa3 into main Jul 19, 2026
6 checks passed
@fazpu
fazpu deleted the context-economy branch July 19, 2026 11:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant