Fall back to Claude Code subscription when ANTHROPIC_API_KEY is unset#629
Open
aarshvir wants to merge 2 commits into
Open
Fall back to Claude Code subscription when ANTHROPIC_API_KEY is unset#629aarshvir wants to merge 2 commits into
aarshvir wants to merge 2 commits into
Conversation
If a user has no Anthropic API key but is logged into the Claude Code CLI, route Anthropic model calls through `claude-agent-sdk` so the run uses their Claude Code subscription instead of erroring out. - Add `ChatClaudeCode`, a minimal LangChain `BaseChatModel` that wraps `claude_agent_sdk.query()`. - `get_model()` returns it for the Anthropic branch when no key is found (lazy import; if the SDK isn't installed, raise an upgraded error). - `has_json_mode()` returns False for Anthropic-without-key so call_llm's existing JSON-extraction path handles structured output. - Other providers unchanged. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
12 tasks
…ion-fallback # Conflicts: # src/llm/models.py
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
If a user has no
ANTHROPIC_API_KEYbut is already logged into the Claude Code CLI locally, this PR routes Anthropic model calls throughclaude-agent-sdkso the run uses their Claude Code subscription instead of erroring out withValueError: Anthropic API key not found.Today the only way to use a Claude model is to pay for the Anthropic API on top of any existing Claude Code / Pro / Max subscription. After this change, a Claude Code login is enough.
What changed
src/llm/claude_code.py—ChatClaudeCode, a minimal LangChainBaseChatModelthat wrapsclaude_agent_sdk.query(). Concatenates messages, pullsSystemMessageinto the SDK'ssystem_prompt, runs the async call viaasyncio.run(or a thread if a loop is already running), and returns the assistant text as anAIMessage. Prints a one-time stderr notice on first use.src/llm/models.py— the Anthropic branch ofget_model()no longer raises on missing key. It lazy-importsChatClaudeCodeand returns it; ifclaude-agent-sdkisn't installed, the error message now tells the user to either set the key or install the SDK.src/llm/models.py—LLMModel.has_json_mode()returnsFalsefor Anthropic whenANTHROPIC_API_KEYis unset, socall_llmskipswith_structured_outputand falls into the existing markdown/brace JSON-extraction path that already serves Gemini and DeepSeek. No changes tocall_llmitself.pyproject.toml— addsclaude-agent-sdk = "^0.1.0"..env.example+README.md— short notes documenting the fallback.Design notes
ANTHROPIC_API_KEYset never import the SDK, so it's effectively a soft dependency.call_llm. Reuses the existing text-parsing fallback path.has_json_mode()readsos.environrather than the web-app's per-requestapi_keysdict. If a web caller passes the key only viaapi_keys(not env), structured output is skipped and JSON extraction kicks in — still produces correct output, just slightly less efficient. Happy to threadapi_keysinto the check if you'd prefer.Test plan
poetry lock && poetry installcleanly resolvesclaude-agent-sdkANTHROPIC_API_KEYset →poetry run python src/main.py --ticker AAPLpicking Claude Opus 4.7 runs viaChatAnthropicas beforeANTHROPIC_API_KEYunset,claudeCLI logged in → same command prints the one-time notice and produces normal trading signalspip uninstall claude-agent-sdkand unset the key → fails fast with the updated error messagepoetry run python src/backtester.py --ticker AAPLunder the fallback path survives the LangGraph loop🤖 Generated with Claude Code