Skip to content

feat: add 1M token context window support for Claude 4+ via env vars#6654

Closed
stevenvo wants to merge 5 commits into
anomalyco:devfrom
stevenvo:feat/anthropic-1m-context-env-var
Closed

feat: add 1M token context window support for Claude 4+ via env vars#6654
stevenvo wants to merge 5 commits into
anomalyco:devfrom
stevenvo:feat/anthropic-1m-context-env-var

Conversation

@stevenvo

@stevenvo stevenvo commented Jan 2, 2026

Copy link
Copy Markdown

Summary

This PR adds support for enabling 1M token context windows for Claude Sonnet 4.5 through environment variables for both Anthropic direct and Vertex AI Anthropic providers.

Note: Currently only Claude Sonnet 4.5 supports 1M token context windows. All other Claude models (Opus 4, Haiku 4.5, etc.) remain at 200K tokens according to models.dev.

Changes

  • Anthropic Direct: Add ANTHROPIC_1M_CONTEXT=true env var to enable 1M context for Sonnet 4.5

    • Preserves existing beta headers (claude-code, interleaved-thinking, fine-grained-tool-streaming)
    • Prepends context-1m-2025-08-07 beta when enabled
  • Vertex AI Anthropic: Add VERTEX_ANTHROPIC_1M_CONTEXT=true env var to enable 1M context for Sonnet 4.5

    • Sets anthropic-beta: context-1m-2025-08-07 header when enabled
  • Model Detection: Regex pattern specifically matches Claude Sonnet 4.5 variants

    • Matches patterns: claude-sonnet-4.5, claude-sonnet-4-5, claude-sonnet-4.5-20250929
    • Automatically sets context limit to 1,000,000 tokens when env var is enabled

Testing

  • ✅ All existing provider tests pass (58 tests)
  • ✅ TypeScript compilation successful
  • ✅ No type errors

Usage

# For Anthropic direct (Sonnet 4.5 only)
export ANTHROPIC_1M_CONTEXT=true

# For Vertex AI Anthropic (Sonnet 4.5 only)
export VERTEX_ANTHROPIC_1M_CONTEXT=true

stevenvo and others added 5 commits December 23, 2025 22:32
…uilds

This commit addresses two issues with the bin/opencode wrapper script:

1. **ES Module Compatibility**: Converts CommonJS require() syntax to ES
   module imports. The package.json specifies "type": "module", which
   causes Node.js to treat .js files as ES modules. While Bun (the
   project's primary runtime) allows require() in ES modules, Node.js
   strictly enforces ES module syntax, causing failures for developers
   using Node.js.

2. **Local Development Support**: Adds check for binaries in dist/ folder
   before looking in node_modules/. When developers build locally with
   'bun run build' and use npm link for testing, the compiled binaries
   are placed in dist/, but the wrapper only searched node_modules/,
   causing it to fail.

These changes improve the developer experience for contributors who:
- Use Node.js instead of Bun for running the wrapper
- Test local builds via npm link or similar methods

The changes are backward compatible - production packages continue to
work as the wrapper still checks node_modules/ as a fallback.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Add ANTHROPIC_1M_CONTEXT env var for Anthropic direct
- Add VERTEX_ANTHROPIC_1M_CONTEXT env var for Vertex AI Anthropic
- Automatically enable 1M context for Claude 4+ models (opus/sonnet/haiku)
- Preserve existing beta headers while adding context-1m-2025-08-07 beta
- Improve model ID matching regex for Claude 4 variant detection
Only Claude Sonnet 4.5 currently supports 1M token context windows.
Updated regex pattern from claude-(opus|sonnet|haiku)-4 to claude-sonnet-4[.-]5
to specifically match only Sonnet 4.5 variants.
@stevenvo stevenvo closed this Jan 2, 2026
@stevenvo
stevenvo deleted the feat/anthropic-1m-context-env-var branch January 2, 2026 17:07
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.

1 participant