Skip to content

fix(agent): bound agent working context with autoCompactWindow#855

Draft
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/agent-auto-compact-window
Draft

fix(agent): bound agent working context with autoCompactWindow#855
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/agent-auto-compact-window

Conversation

@posthog

@posthog posthog Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Problem

On 2026-07-09 around noon Pacific, the wizard's claude-sonnet-4-6 per-generation input context jumped from the prior ~110K band to ~400K+ and daily spend on the model roughly doubled — a sustained cost regression reaching hundreds of users.

Root cause: the anthropic harness enables the 1M-context beta (context-1m-2025-08-07) so compaction has physical headroom to succeed on large projects, but it set no bound on the working-context size. The beta has been requested since April, but once the LLM gateway began honoring it, the SDK's auto-compaction ceiling effectively moved from ~200K toward ~1M. Long linear runs then stopped compacting at a sane point — per-generation input climbed past 600K on the most expensive traces — and per-generation cost more than doubled.

Confirmed against production $ai_generation data: the tool-definitions payload was unchanged across the boundary (ruling out the MCP tools-mode pin), while pre-noon traces showed a compaction sawtooth bounded near ~85–180K and post-noon traces grew monotonically with no compaction.

Changes

  • Pass settings.autoCompactWindow (new AGENT_AUTO_COMPACT_WINDOW, 200K) to the SDK query() in the anthropic harness. This decouples the working window the model pays for from the physical window: the 1M beta still gives the compaction step room to run, while auto-compaction now fires at a bounded size — returning per-generation input to roughly the pre-1M band.
  • Extract the beta string into a shared CONTEXT_1M_BETA constant and reuse it at the three sites that set it.
  • Note in code that the pi harness drives its own compaction (pi-coding-agent), so this SDK guard does not apply there — flagged as a separate follow-up.

Test plan

  • New unit test guards the constant against silently-dropped out-of-range values (the SDK validates autoCompactWindow to 100K–1M and silently ignores values outside it, which would quietly reinstate the regression).
  • pnpm build && pnpm test — 1307 tests pass; pnpm lint — 0 errors.
  • Field verification: after rollout, confirm wizard claude-sonnet-4-6 per-generation input tokens return near the ~110K band.

Created with PostHog Code from an inbox report.

The anthropic harness enables the 1M-context beta so compaction has room to
run on large projects, but set no working-context bound. Once the LLM gateway
began honoring the beta, the SDK deferred auto-compaction toward the 1M ceiling,
so long linear runs' per-generation input climbed past 600K and per-generation
cost more than doubled.

Pin `settings.autoCompactWindow` to 200K so compaction fires at a bounded
working size while the 1M physical window still gives the compaction step
headroom, returning per-generation input to the prior band.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

Generated-By: PostHog Code
Task-Id: 3584c98f-82b2-4d62-867c-ed7d99705d99
@github-actions

Copy link
Copy Markdown

🧙 Wizard CI

Run the Wizard CI and test your changes against wizard-workbench example apps by replying with a GitHub comment using one of the following commands:

Test all apps:

  • /wizard-ci all

Test all apps in a directory:

  • /wizard-ci basic-integration
  • /wizard-ci error-tracking-upload-source-maps
  • /wizard-ci mcp-analytics
  • /wizard-ci misc
  • /wizard-ci revenue

Test an individual app:

  • /wizard-ci basic-integration/android
  • /wizard-ci basic-integration/angular
  • /wizard-ci basic-integration/astro
Show more apps
  • /wizard-ci basic-integration/django
  • /wizard-ci basic-integration/fastapi
  • /wizard-ci basic-integration/flask
  • /wizard-ci basic-integration/javascript-node
  • /wizard-ci basic-integration/javascript-web
  • /wizard-ci basic-integration/laravel
  • /wizard-ci basic-integration/next-js
  • /wizard-ci basic-integration/nuxt
  • /wizard-ci basic-integration/python
  • /wizard-ci basic-integration/rails
  • /wizard-ci basic-integration/react-native
  • /wizard-ci basic-integration/react-router
  • /wizard-ci basic-integration/sveltekit
  • /wizard-ci basic-integration/swift
  • /wizard-ci basic-integration/tanstack-router
  • /wizard-ci basic-integration/tanstack-start
  • /wizard-ci basic-integration/vue
  • /wizard-ci error-tracking-upload-source-maps/android
  • /wizard-ci error-tracking-upload-source-maps/cicd-docker-node-raw
  • /wizard-ci error-tracking-upload-source-maps/cicd-github-actions-docker-node-raw
  • /wizard-ci error-tracking-upload-source-maps/cicd-github-actions-nested-docker-node-raw
  • /wizard-ci error-tracking-upload-source-maps/cicd-github-actions-node-raw
  • /wizard-ci error-tracking-upload-source-maps/cicd-github-actions-single-stage-docker-node-raw
  • /wizard-ci error-tracking-upload-source-maps/cicd-gitlab-node-raw
  • /wizard-ci error-tracking-upload-source-maps/cicd-monorepo-pnpm-node-react
  • /wizard-ci error-tracking-upload-source-maps/cicd-monorepo-raw-node-react
  • /wizard-ci error-tracking-upload-source-maps/cicd-ssh-vps-node-raw
  • /wizard-ci error-tracking-upload-source-maps/flutter
  • /wizard-ci error-tracking-upload-source-maps/ios
  • /wizard-ci error-tracking-upload-source-maps/next
  • /wizard-ci error-tracking-upload-source-maps/next-no-posthog
  • /wizard-ci error-tracking-upload-source-maps/node-raw
  • /wizard-ci error-tracking-upload-source-maps/node-rollup
  • /wizard-ci error-tracking-upload-source-maps/node-rollup-typescript-plugin
  • /wizard-ci error-tracking-upload-source-maps/node-webpack
  • /wizard-ci error-tracking-upload-source-maps/nuxt-3-6
  • /wizard-ci error-tracking-upload-source-maps/nuxt-4-3
  • /wizard-ci error-tracking-upload-source-maps/react-native
  • /wizard-ci error-tracking-upload-source-maps/react-vite
  • /wizard-ci error-tracking-upload-source-maps/rust
  • /wizard-ci mcp-analytics/custom-dispatcher
  • /wizard-ci mcp-analytics/typescript-sdk
  • /wizard-ci misc/quack-quack
  • /wizard-ci revenue/stripe

Results will be posted here when complete.

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.

0 participants