feat: add Anthropic Context Editing API support#1147
Merged
Conversation
Persist OAuth/setup tokens in ANTHROPIC_TOKEN instead of ANTHROPIC_API_KEY. Reserve ANTHROPIC_API_KEY for regular Console API keys. Changes: - anthropic_adapter: reorder resolve_anthropic_token() priority — ANTHROPIC_TOKEN first, ANTHROPIC_API_KEY as legacy fallback - config: add save_anthropic_oauth_token() / save_anthropic_api_key() helpers that clear the opposing slot to prevent priority conflicts - config: show_config() prefers ANTHROPIC_TOKEN for display - setup: OAuth login and pasted setup-tokens write to ANTHROPIC_TOKEN - setup: API key entry writes to ANTHROPIC_API_KEY and clears ANTHROPIC_TOKEN - main: same fixes in _run_anthropic_oauth_flow() and _model_flow_anthropic() - main: _has_any_provider_configured() checks ANTHROPIC_TOKEN - doctor: use _is_oauth_token() for correct auth method validation - runtime_provider: updated error message - run_agent: simplified client init to use resolve_anthropic_token() - run_agent: updated 401 troubleshooting messages - status: prefer ANTHROPIC_TOKEN in status display - tests: updated priority test, added persistence helper tests Cherry-picked from PR #1141 by kshitijk4poor, rebased onto current main with unrelated changes (web_policy config, blocklist CLI) removed. Co-authored-by: kshitijk4poor <kshitijk4poor@users.noreply.github.com>
048c0a9 to
bb3f5ed
Compare
angelburgosrosado
pushed a commit
to angelburgosrosado/hermes-agent
that referenced
this pull request
Apr 27, 2026
…6ec3b1a9 fix: separate Anthropic OAuth tokens from API keys
02356abc
pushed a commit
to 02356abc/hermes-agent
that referenced
this pull request
May 14, 2026
…6ec3b1a9 fix: separate Anthropic OAuth tokens from API keys
olympus-terminal
pushed a commit
to olympus-terminal/hermes-agent
that referenced
this pull request
May 16, 2026
…6ec3b1a9 fix: separate Anthropic OAuth tokens from API keys
Egavasyug
pushed a commit
to Egavasyug/hermes-agent
that referenced
this pull request
Jun 10, 2026
…6ec3b1a9 fix: separate Anthropic OAuth tokens from API keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Refs #526, supersedes #528.
Integrates Anthropic's server-side context management (beta:
context-management-2025-06-27). When enabled, the API automatically clears old tool use/result pairs and thinking blocks after prompt cache lookup but before token counting — preserving cache prefixes while freeing context space.How it works
The client sends the full unmodified message array + a
context_managementparameter:This is impossible with client-side stripping, which modifies the message array and breaks cache prefixes.
Implementation
context-management-2025-06-27beta header.build_anthropic_kwargs()buildscontext_managementedits viaextra_body.clear_thinkingonly included when reasoning is enabled (API requirement).context_editingconfig throughAIAgentto the adaptercontext_editingconfig from config.yamlcontext_editingsection inDEFAULT_CONFIGConfig (opt-in)
Live tested with Anthropic API
context_management— accepted, response normalclear_thinkingcorrectly omitted when reasoning is disabledAIAgent._build_api_kwargs()Notes
provider: anthropic. OpenRouter passthrough is unverified.context_editing.enabled: true