Context Close to Limit
Most bugs in long sessions with an AI agent start precisely when the context window is filled to 90%+. The model begins to “lose” system prompt instructions and tools. The signal is usage.input_tokens relative to the model limit:
LIMITS = { "claude-sonnet-4-5": 200_000, "claude-opus-4-5": 200_000, "gpt-4.1": 1_000_000, "gpt-4o": 128_000,}
def observe(model, used_tokens): limit = LIMITS.get(model, 200_000) ratio = used_tokens / limit flag = f"/tmp/ctx-{model}.flag" if ratio >= 0.85 and not os.path.exists(flag): push(f"🧠 Context at {int(ratio*100)}% for {model}", f"Used {used_tokens:,} of {limit:,} tokens.\n" "Summarize or start a new thread.", priority=7) open(flag, "w").close() elif ratio < 0.5 and os.path.exists(flag): os.unlink(flag) # reset flag after summaryIt’s convenient to call observe() in a common wrapper over the SDK — this control then works automatically for any long dialogue.