#20104: Fix Anthropic 1M model ID: strip -1m suffix before API call
agents
size: S
trusted-contributor
Cluster:
Context Window and Model Updates
## Problem
When using the `-1m` model ID convention (e.g. `claude-opus-4-6-1m`), the suffix is sent directly to the Anthropic API, resulting in a 404 error. The API doesn't recognize `claude-opus-4-6-1m` as a valid model name.
The codebase already has partial 1M support (`isAnthropic1MModel()`, beta header injection), but the model ID wasn't being normalized before the API call.
## Fix
- Strip `-1m` suffix from the model ID in `createAnthropicBetaHeadersWrapper` before forwarding to the Anthropic API
- Auto-detect `-1m` suffix to inject the `context-1m-2025-08-07` beta header without requiring an explicit `context1m` param
- `isAnthropic1MModel()` now recognizes `-1m` suffixed model IDs (strips suffix before prefix check)
## Testing
6 new tests covering:
- Suffix stripping for Opus and Sonnet models
- Case-insensitive suffix handling
- Auto-injection of beta header from suffix
- Non-anthropic providers are not affected
- Explicit `context1m` param still works without suffix
Fixes #20079
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR fixes a 404 error when using the `-1m` model ID convention (e.g., `claude-opus-4-6-1m`) by stripping the `-1m` suffix before sending the model ID to the Anthropic API, and auto-detecting the suffix to inject the `context-1m-2025-08-07` beta header. The change completes the existing partial 1M support in the codebase.
- Two new helper functions (`stripAnthropic1MSuffix`, `hasAnthropic1MSuffix`) centralize suffix detection and removal logic
- `isAnthropic1MModel()` updated to strip the suffix before checking model prefixes
- `resolveAnthropicBetas()` now auto-detects `-1m` suffix in addition to the explicit `context1m` param
- `createAnthropicBetaHeadersWrapper()` strips the `-1m` suffix from the model object before forwarding to the underlying stream function
- 6 well-structured tests covering suffix stripping, case-insensitive handling, auto-injection, and provider isolation
- **Issue**: `stripAnthropic1MSuffix` doesn't `.trim()` while `hasAnthropic1MSuffix` does, creating an inconsistency that could cause the suffix to not be stripped when it's detected
- **Issue**: Non-opus/sonnet models with `-1m` suffix (e.g., `claude-haiku-3-5-1m`) will still 404 since the suffix is only stripped inside the beta headers wrapper, which isn't created when 1M is unsupported
<h3>Confidence Score: 3/5</h3>
- This PR solves the main problem but has minor correctness issues worth addressing before merge.
- The core fix is sound and well-tested for the primary use case (opus/sonnet with -1m suffix). However, the trim inconsistency between stripAnthropic1MSuffix and hasAnthropic1MSuffix could cause the suffix to remain unstripped in edge cases, and unsupported model types with -1m suffix will still 404 without clear user feedback beyond a warning log.
- src/agents/pi-embedded-runner/extra-params.ts - review the trim inconsistency and unsupported model edge case
<sub>Last reviewed commit: 03048c4</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#19927: fix(agents): replace hardcoded Anthropic model IDs with pattern mat...
by Milofax · 2026-02-18
76.8%
#10831: fix(agents): set Opus 4.6 context window to 1M in forward-compat fa...
by slawt · 2026-02-07
75.9%
#20747: feat(copilot): support Claude models with correct context windows (...
by yuf1011 · 2026-02-19
74.5%
#11198: fix(models): strip @profile suffix from model selection
by mcaxtr · 2026-02-07
74.2%
#10108: fix: override stale Anthropic OAuth stealth headers for Opus 4.6
by CivilBooks · 2026-02-06
74.1%
#21517: fix: preserve pi-ai default betas when context1m/anthropicBeta over...
by ManuelFerreras · 2026-02-20
74.0%
#12220: fix: forward-compat models now respect user-configured contextWindow
by Batuhan4 · 2026-02-09
73.7%
#19407: fix(agents): strip thinking blocks on cross-provider model switch (...
by lailoo · 2026-02-17
73.7%
#23542: fix/hf inference
by Josephrp · 2026-02-22
73.2%
#13976: fix(anthropic): include Anthropic in tool call ID sanitization
by omair445 · 2026-02-11
73.0%