Skip to content

feat(anthropic): migrate 1M context from beta to GA#45613

Open
haoyu-haoyu wants to merge 3 commits intoopenclaw:mainfrom
haoyu-haoyu:feat/anthropic-1m-context-ga-migration
Open

feat(anthropic): migrate 1M context from beta to GA#45613
haoyu-haoyu wants to merge 3 commits intoopenclaw:mainfrom
haoyu-haoyu:feat/anthropic-1m-context-ga-migration

Conversation

@haoyu-haoyu
Copy link
Copy Markdown

Summary

Anthropic has officially graduated the 1M context window from beta to GA. This PR implements Phase 1 of #45550:

  • Stop injecting beta header: context-1m-2025-08-07 is no longer added to anthropic-beta when context1m: true
  • Remove OAuth skip logic: OAuth tokens (sk-ant-oat-*) now support 1M context natively — the special-case filtering that stripped the context-1m beta for OAuth is removed
  • Strip legacy header: If users still have context-1m-2025-08-07 in their anthropicBeta config, it is silently removed to prevent sending a stale header
  • Remove dead code: isAnthropic1MModel(), ANTHROPIC_1M_MODEL_PREFIXES, and the unused log import are removed from the stream wrappers

Backward compatibility

Fully backward compatible:

  • context1m: true continues to work for context window sizing (resolveContextTokensForModel in context.ts)
  • Existing configs with context1m: true will silently stop sending the unnecessary beta header
  • No user-facing behavior change — 1M context works the same, just without the beta header

Files changed

File Change
src/agents/pi-embedded-runner/anthropic-stream-wrappers.ts Remove beta header injection, OAuth skip logic, dead code
src/agents/pi-embedded-runner-extraparams.test.ts Update 4 test cases to reflect GA behavior

Test plan

  • All 79 extraparams tests pass (pnpm test -- src/agents/pi-embedded-runner-extraparams.test.ts)
  • TypeScript compiles cleanly (no new errors)
  • Lint/format pass

Closes #45550 (Phase 1)

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S labels Mar 14, 2026
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Mar 14, 2026

Greptile Summary

This PR migrates Anthropic's 1M context window from beta to GA by removing the context-1m-2025-08-07 beta header injection, eliminating the OAuth special-case filtering, and deleting the now-dead isAnthropic1MModel() / ANTHROPIC_1M_MODEL_PREFIXES helpers. Backward compatibility is preserved via a silent strip of the legacy header if users still have it in their anthropicBeta config.

The change is straightforward and the tests are well-updated. One area worth a closer look:

  • OAuth beta injection gap: createAnthropicBetaHeadersWrapper (which injects PI_AI_OAUTH_ANTHROPIC_BETAS) is only applied when resolveAnthropicBetas returns a non-empty array. With this PR, users who have context1m: true but no explicit anthropicBeta will no longer trigger the wrapper, so oauth-2025-04-20 and claude-code-20250219 are not pre-populated. The old test explicitly guarded against pi-ai's mergeHeaders stripping those betas; the new test silently drops that protection. If pi-ai now injects OAuth betas unconditionally this is fine, but that assumption isn't confirmed in the PR description or test comments.

Confidence Score: 4/5

  • Safe to merge after confirming pi-ai independently injects OAuth betas for OAuth-keyed requests.
  • The changes are minimal, well-motivated, and tests are updated consistently. The only open question is whether removing the createAnthropicBetaHeadersWrapper invocation path for OAuth+context1m users (with no explicit anthropicBeta config) silently drops required OAuth betas that pi-ai previously relied on being pre-populated. If pi-ai handles those betas independently, the score would be 5.
  • src/agents/pi-embedded-runner-extraparams.test.ts — specifically the OAuth test case at line 1504 where the expectation changed from requiring oauth-2025-04-20/claude-code-20250219 to expecting no beta header at all.
Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-runner-extraparams.test.ts
Line: 1504-1507

Comment:
**OAuth betas silently dropped when `context1m` is the only configured beta**

The old test comment read: _"Must include the OAuth-required betas so they aren't stripped by pi-ai's mergeHeaders"_ — meaning `oauth-2025-04-20` and `claude-code-20250219` had to be pre-populated before pi-ai's own `mergeHeaders` call would clobber the `anthropic-beta` header.

In `extra-params.ts` the beta wrapper is only applied when `resolveAnthropicBetas` returns a non-empty array (line 367: `if (anthropicBetas?.length)`). With `context1m: true` and **no explicit `anthropicBeta`** config, `resolveAnthropicBetas` now returns `undefined`, so `createAnthropicBetaHeadersWrapper` is **never called**. That means `PI_AI_OAUTH_ANTHROPIC_BETAS` (which lives inside that wrapper) is never injected for this specific combination.

The scenarios and their outcomes:

| Config | Token type | Before this PR | After this PR |
|---|---|---|---|
| `context1m: true`, no `anthropicBeta` | API key | wrapper applied, default betas injected | wrapper **skipped**, no betas injected |
| `context1m: true`, no `anthropicBeta` | OAuth | wrapper applied, **oauth betas** injected | wrapper **skipped**, no oauth betas |
| `context1m: true`, `anthropicBeta: [...]` | OAuth | wrapper applied, oauth betas injected | wrapper still applied (user betas present), oauth betas still injected ✓ |

If pi-ai's `streamSimple` independently injects the oauth betas via its own mechanism, this is a no-op. But if pi-ai relied on them being pre-populated in the headers (to survive its `mergeHeaders` pass), OAuth+context1m users without an explicit `anthropicBeta` config could start seeing `401 / invalid beta` errors.

Could you confirm whether pi-ai now adds `oauth-2025-04-20` / `claude-code-20250219` unconditionally for OAuth keys, making the pre-population redundant? If not, the fix would be to always apply the wrapper for Anthropic regardless of whether user betas are empty, e.g.:

```typescript
// extra-params.ts
const anthropicBetas = resolveAnthropicBetas(merged, provider, modelId);
if (provider === "anthropic" || anthropicBetas?.length) {
  agent.streamFn = createAnthropicBetaHeadersWrapper(agent.streamFn, anthropicBetas ?? []);
}
```

How can I resolve this? If you propose a fix, please make it concise.

Last reviewed commit: 90bc2f2

Comment thread src/agents/pi-embedded-runner-extraparams.test.ts Outdated
@clawsweeper
Copy link
Copy Markdown
Contributor

clawsweeper Bot commented Apr 28, 2026

Codex review: needs maintainer review before merge.

Summary
The PR removes context-1m-2025-08-07 emission for context1m, strips legacy configured Anthropic betas, preserves default/OAuth Anthropic beta headers, and updates related tests, docs, and changelog.

Reproducibility: yes. Source and test inspection against current main shows context1m: true for Anthropic Opus/Sonnet still appends context-1m-2025-08-07, and docs/tests still assert beta-gated behavior.

Next step before merge
No separate automated repair is indicated; the remaining action is maintainer review and exact-head validation for this open implementation PR.

Security
Cleared: The diff touches Anthropic request-header logic, docs, tests, and changelog only; it adds no dependencies, workflows, secret handling, package resolution, or generated artifacts.

Review details

Best possible solution:

Land this repaired PR after maintainer review and exact-head checks, while keeping broader OAuth-wrapper work such as #41461 as a separate follow-up instead of expanding this Phase 1 migration.

Do we have a high-confidence way to reproduce the issue?

Yes. Source and test inspection against current main shows context1m: true for Anthropic Opus/Sonnet still appends context-1m-2025-08-07, and docs/tests still assert beta-gated behavior.

Is this the best way to solve the issue?

Yes for this PR's Phase 1 scope. The PR is the narrow maintainable fix because it removes the retired 1M beta behavior, keeps context1m for context sizing compatibility, and preserves pi-ai default/OAuth betas where header merging needs them.

Acceptance criteria:

  • pnpm test extensions/anthropic/stream-wrappers.test.ts src/agents/pi-embedded-runner-extraparams.test.ts
  • pnpm check:changed

What I checked:

  • Current main still injects the legacy beta: On current main, resolveAnthropicBetas() still adds context-1m-2025-08-07 when context1m is true for Opus/Sonnet-family Anthropic models. (extensions/anthropic/stream-wrappers.ts:100, 36c047c0264e)
  • Current main still documents beta-gated behavior: The provider docs still call the 1M context window beta-gated and say OpenClaw maps context1m to the context-1m-2025-08-07 beta header. Public docs: docs/providers/anthropic.md. (docs/providers/anthropic.md:266, 36c047c0264e)
  • PR head strips the retired beta: At PR head, resolveAnthropicBetas() parses configured betas, deletes the retired context beta, and returns only the remaining configured values. (extensions/anthropic/stream-wrappers.ts:115, ba9b5bf41d00)
  • PR head preserves the OAuth/default-beta wrapper path: The repaired head still applies the Anthropic beta wrapper when context1m or legacy beta config is the only trigger, so required pi-ai default/OAuth betas are preserved instead of being dropped. (extensions/anthropic/stream-wrappers.ts:209, ba9b5bf41d00)
  • PR head covers the repaired edge cases: The PR adds tests for stripping legacy context-1m betas from config and preserving OAuth-required betas when context1m or the legacy beta is the only trigger. (extensions/anthropic/stream-wrappers.test.ts:119, ba9b5bf41d00)
  • Review discussion acknowledged and repaired the OAuth gap: Greptile flagged the possible OAuth beta drop, and Vincent's later comment says ProjectClownfish pushed a narrow repair to this branch with pnpm check:changed validation. (ba9b5bf41d00)

Likely related people:

  • steipete: Peter introduced the original Anthropic 1M beta-header support and has recent maintenance commits in the Anthropic stream-wrapper area. (role: introduced behavior / recent Anthropic provider maintainer; confidence: high; commits: c90b09cb02b2, 599b1b84620b, 4f7038ae33ae; files: extensions/anthropic/stream-wrappers.ts, docs/providers/anthropic.md, docs/reference/token-use.md)
  • vincentkoc: Vincent authored the merged OAuth context1m skip work, updated Claude CLI context1m docs, and pushed the narrow repair described in this PR discussion. (role: OAuth/context1m behavior owner and recent branch repair owner; confidence: high; commits: f03ff3975406, 7fef13abbc67, ba9fd564e07c; files: extensions/anthropic/stream-wrappers.ts, extensions/anthropic/stream-wrappers.test.ts, docs/providers/anthropic.md)
  • bidadh: The current changelog credits @bidadh for the Claude CLI context1m sizing follow-up, which is adjacent to this PR's promise to keep context1m as a sizing opt-in. (role: adjacent Claude CLI context sizing contributor; confidence: medium; files: CHANGELOG.md, src/agents/context.ts, src/agents/context.test.ts)

Remaining risk / open question:

  • Exact-head CI/check status was not available in the provided context, so merge should still be gated by the targeted Anthropic tests and changed check.

Codex review notes: model gpt-5.5, reasoning high; reviewed against 36c047c0264e.

haoyu-haoyu and others added 3 commits April 28, 2026 07:23
Anthropic has graduated the 1M context window from beta to GA.
This commit:

- Stops injecting the context-1m-2025-08-07 beta header when
  context1m: true is configured
- Removes the OAuth token skip logic that was needed because
  Anthropic previously rejected the context-1m beta with OAuth auth
  (OAuth now supports 1M natively)
- Strips the legacy beta header from user-configured anthropicBeta
  arrays to prevent sending a stale header
- Removes the now-unused isAnthropic1MModel helper,
  ANTHROPIC_1M_MODEL_PREFIXES constant, and logger import from
  the stream wrappers

The context1m config param continues to be respected for context
window sizing in context.ts — only the beta header injection is
removed.

Closes openclaw#45550 (Phase 1)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@vincentkoc
Copy link
Copy Markdown
Member

ProjectClownfish pushed a narrow repair to this branch so the original contributor path can stay canonical.

Source PR: #45613
Validation: pnpm check:changed
Contributor credit is preserved in the branch history and PR context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling clawsweeper Tracked by ClawSweeper automation docs Improvements or additions to documentation extensions: anthropic gateway Gateway runtime size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(anthropic): migrate 1M context from beta to GA

2 participants