From ac0af56b3df554def9f36f0e7a993c0222784d46 Mon Sep 17 00:00:00 2001 From: Alessandro Pogliaghi Date: Fri, 10 Jul 2026 09:26:38 +0100 Subject: [PATCH 1/3] fix(agent): stop resume hydration from overfilling the context window Cold-resume JSONL hydration selected up to 800k estimated tokens of history at 4 chars/token, but JSON-heavy tool payloads tokenize at ~2.5-3 chars/token, so the rebuilt transcript could exceed the model's real context window before the run even started. Because the hydrated messages carry zero usage metadata, the SDK cannot auto-compact ahead of the first API call, and every request (including compaction itself) fails with "prompt is too long", permanently bricking the task on every subsequent resume. Estimate at 3 chars/token and target roughly half the window (400k estimated for 1M-context models, 80k for 200k), leaving room for the system prompt, tools, skills, and the resumed run's own work. Generated-By: PostHog Code Task-Id: c6065620-5694-4478-a20a-fec4ccde8f1a --- .../adapters/claude/session/jsonl-hydration.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/agent/src/adapters/claude/session/jsonl-hydration.ts b/packages/agent/src/adapters/claude/session/jsonl-hydration.ts index 9bc9c268b1..3b7bb50748 100644 --- a/packages/agent/src/adapters/claude/session/jsonl-hydration.ts +++ b/packages/agent/src/adapters/claude/session/jsonl-hydration.ts @@ -241,9 +241,18 @@ export function rebuildConversation( return turns; } -const CHARS_PER_TOKEN = 4; -const DEFAULT_MAX_TOKENS = 150_000; -const LARGE_CONTEXT_MAX_TOKENS = 800_000; +// JSON-heavy tool payloads tokenize at ~2.5-3 chars/token, so estimate low: +// overestimating a turn's cost just drops more history, underestimating +// overfills the context window. +const CHARS_PER_TOKEN = 3; +// Budgets must leave the window room for the system prompt, tools, skills, +// and the resumed run's own work. The rebuilt transcript carries zero usage +// metadata, so the SDK can't see the real context size and auto-compact +// before the first API call — if the hydrated history overfills the window, +// every request (including compaction itself) fails with "prompt is too +// long" and the run dies. Target roughly half the window in real tokens. +const DEFAULT_MAX_TOKENS = 80_000; +const LARGE_CONTEXT_MAX_TOKENS = 400_000; function estimateTurnTokens(turn: ConversationTurn): number { let chars = 0; From f4a81d2fafb3c799db1e352593229b8dc9bfa2bd Mon Sep 17 00:00:00 2001 From: Alessandro Pogliaghi Date: Fri, 10 Jul 2026 09:49:21 +0100 Subject: [PATCH 2/3] chore(agent): trim hydration budget comments Generated-By: PostHog Code Task-Id: c6065620-5694-4478-a20a-fec4ccde8f1a --- .../src/adapters/claude/session/jsonl-hydration.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/packages/agent/src/adapters/claude/session/jsonl-hydration.ts b/packages/agent/src/adapters/claude/session/jsonl-hydration.ts index 3b7bb50748..1bb13002b8 100644 --- a/packages/agent/src/adapters/claude/session/jsonl-hydration.ts +++ b/packages/agent/src/adapters/claude/session/jsonl-hydration.ts @@ -241,16 +241,10 @@ export function rebuildConversation( return turns; } -// JSON-heavy tool payloads tokenize at ~2.5-3 chars/token, so estimate low: -// overestimating a turn's cost just drops more history, underestimating -// overfills the context window. +// JSON-heavy tool payloads tokenize at ~2.5-3 chars/token, so estimate low. const CHARS_PER_TOKEN = 3; -// Budgets must leave the window room for the system prompt, tools, skills, -// and the resumed run's own work. The rebuilt transcript carries zero usage -// metadata, so the SDK can't see the real context size and auto-compact -// before the first API call — if the hydrated history overfills the window, -// every request (including compaction itself) fails with "prompt is too -// long" and the run dies. Target roughly half the window in real tokens. +// Target ~half the context window: hydrated transcripts carry no usage +// metadata, so the SDK can't auto-compact before the first API call. const DEFAULT_MAX_TOKENS = 80_000; const LARGE_CONTEXT_MAX_TOKENS = 400_000; From ec17957414084a5a5080e1032a086875bc197f0e Mon Sep 17 00:00:00 2001 From: Alessandro Pogliaghi Date: Fri, 10 Jul 2026 10:46:54 +0100 Subject: [PATCH 3/3] chore(agent): state budget rationale independent of usage seeding Generated-By: PostHog Code Task-Id: c6065620-5694-4478-a20a-fec4ccde8f1a --- packages/agent/src/adapters/claude/session/jsonl-hydration.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/agent/src/adapters/claude/session/jsonl-hydration.ts b/packages/agent/src/adapters/claude/session/jsonl-hydration.ts index 1bb13002b8..2f5f39dfc2 100644 --- a/packages/agent/src/adapters/claude/session/jsonl-hydration.ts +++ b/packages/agent/src/adapters/claude/session/jsonl-hydration.ts @@ -243,8 +243,8 @@ export function rebuildConversation( // JSON-heavy tool payloads tokenize at ~2.5-3 chars/token, so estimate low. const CHARS_PER_TOKEN = 3; -// Target ~half the context window: hydrated transcripts carry no usage -// metadata, so the SDK can't auto-compact before the first API call. +// Target ~half the context window, leaving headroom for the system prompt, +// tools, skills, estimation error, and the resumed run's own work. const DEFAULT_MAX_TOKENS = 80_000; const LARGE_CONTEXT_MAX_TOKENS = 400_000;