Skip to content

Commit ac0af56

Browse files
authored
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
1 parent 886d163 commit ac0af56

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

packages/agent/src/adapters/claude/session/jsonl-hydration.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,18 @@ export function rebuildConversation(
241241
return turns;
242242
}
243243

244-
const CHARS_PER_TOKEN = 4;
245-
const DEFAULT_MAX_TOKENS = 150_000;
246-
const LARGE_CONTEXT_MAX_TOKENS = 800_000;
244+
// JSON-heavy tool payloads tokenize at ~2.5-3 chars/token, so estimate low:
245+
// overestimating a turn's cost just drops more history, underestimating
246+
// overfills the context window.
247+
const CHARS_PER_TOKEN = 3;
248+
// Budgets must leave the window room for the system prompt, tools, skills,
249+
// and the resumed run's own work. The rebuilt transcript carries zero usage
250+
// metadata, so the SDK can't see the real context size and auto-compact
251+
// before the first API call — if the hydrated history overfills the window,
252+
// every request (including compaction itself) fails with "prompt is too
253+
// long" and the run dies. Target roughly half the window in real tokens.
254+
const DEFAULT_MAX_TOKENS = 80_000;
255+
const LARGE_CONTEXT_MAX_TOKENS = 400_000;
247256

248257
function estimateTurnTokens(turn: ConversationTurn): number {
249258
let chars = 0;

0 commit comments

Comments
 (0)