Repository navigation
feat(cli): accept an Anthropic key at every CLI entry point - #385
Merged
neuromechanist merged 3 commits intoSep 17, 2026
Conversation
get_effective_anthropic_key() only ever read ANTHROPIC_API_KEY and the saved Anthropic slot, while get_effective_config() resolved -k/--api-key into the OpenRouter slot regardless of what the key was. An Anthropic key typed at the flag therefore went out on X-OpenRouter-Key, where the server cannot use it. Replace it with get_effective_byok_keys(), which returns both slots, and classify_api_key(), which reads the provider off the prefix the way the widget's inferKeyProvider does. An explicit flag wins outright and clears the other slot, because OSAClient prefers Anthropic when both are present: otherwise an exported ANTHROPIC_API_KEY would silently override a typed OpenRouter key and bill the wrong account. An unrecognized prefix stays OpenRouter so pre-migration credentials keep working. get_effective_config() is left alone; it still resolves the API URL and its single-key return is pinned by existing tests. Tested: 9 new tests in tests/test_cli/test_config.py, full unit suite.
…rror The X-Anthropic-API-Key header was wired end to end in phase 2, but no CLI entry point could put a key on it: `osa init` prompted for an OpenRouter key and saved whatever it got into the OpenRouter slot, `config set` had no --anthropic-key at all, and `osa mirror` refused to run for a user with only an Anthropic key configured. The only working setup was to hand-edit credentials.yaml or export ANTHROPIC_API_KEY in every shell. - init names Anthropic first, prompts for "API key" rather than for one provider's key, and routes it by prefix. Its connection test is now gated on either slot and passes the Anthropic key through, so the test request goes out on the header the server reads. - config set gains --anthropic-key; the two key flags do not clobber each other. - ask, chat and mirror resolve both slots via get_effective_byok_keys(). - --api-key help text across all commands now names both providers. Tested: 6 new tests, including one asserting init's own connection test carries X-Anthropic-API-Key and not X-OpenRouter-Key. Verified against reverted source: 2 of the 4 init tests fail, so they are load-bearing. Full unit suite 2021 passed, 1 pre-existing Rich-ANSI failure in test_version_shows_version that also fails on the epic branch.
Review findings on PR #385. tests/test_cli/test_mirror.py did not exist, so mirror's _get_client fix had no coverage at all. Five tests now assert on the outgoing request rather than on the client object, because a client built with the right key and the wrong header name would still pass the weaker check. Includes the OpenRouter path and the no-key error path, so the two-provider resolution cannot make either unreachable. chat resolves its keys independently of ask, so it gets its own header test. Its response body is serialized from the server's ChatResponse model instead of hand-written: the first attempt hand-wrote message as a string, but it is a nested ChatMessage and the CLI reads response["message"]["content"]. Verified all four new Anthropic-path tests fail when the fix is reverted. README named openrouter.ai as the only place to get a key and showed only --openrouter-key. Also documents anthropic_api_key_env_var alongside the OpenRouter one for communities funding their own usage. classify_api_key's docstring claimed to mirror the widget's inferKeyProvider. It does not: the widget validates the full key shape by regex and returns null for anything unrecognized, while the CLI falls back to OpenRouter so pre-migration configs keep working. The docstring now states the difference and why.
Member
Author
|
All four review findings addressed in c7dab72.
Verified all four new Anthropic-path tests fail when the |
neuromechanist
merged commit Sep 17, 2026
84de2ed
into
feature/issue-360-epic-anthropic-platform
3 of 4 checks passed
This was referenced Sep 17, 2026
Merged
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.
Closes #384.
Problem
Phase 2 wired
X-Anthropic-API-Keyend to end, but no CLI entry point could put a key on it.osa initprompted for "OpenRouter API key" and saved whatever it got into the OpenRouter slot. An Anthropic key typed there went out onX-OpenRouter-Key, where the server cannot use it.osa config sethad no--anthropic-keyflag at all.osa mirrorbuilt its client from the OpenRouter slot only, so it refused to run with "No API key configured" for a user who had only an Anthropic key.-k/--api-keyonaskandchatresolved into the OpenRouter slot regardless of the key's actual provider.The only working Anthropic setup was to hand-edit
credentials.yamlor exportANTHROPIC_API_KEYin every shell.Change
The provider comes from the key's own prefix, mirroring the widget's
inferKeyProvider:classify_api_key(key)returns"anthropic"forsk-ant-, else"openrouter". An unrecognized shape stays OpenRouter, because every key saved before this migration was an OpenRouter key and defaulting the other way would break a pre-existingcredentials.yaml.get_effective_byok_keys(api_key)replacesget_effective_anthropic_key()and returns both slots. An explicit flag wins outright and clears the other slot:OSAClientprefers Anthropic whenever both are present, so without that an exportedANTHROPIC_API_KEYwould silently override a key typed on the command line and bill the wrong account.get_effective_config()is untouched. It still resolves the API URL, and its single-key return is pinned by existing tests.initnames Anthropic first, prompts for "API key" rather than for one provider's, routes by prefix, and gates its connection test on either slot while passing the Anthropic key through.config setgains--anthropic-key; the two key flags do not clobber each other.ask,chatandmirrorresolve both slots.--api-keyhelp text across every command now names both providers.Why this blocks the docs
cli-reference.mdandgetting-started.mdin OpenScience-Collective/documentation#37 cannot describe key setup accurately without either this fix or a note that the CLI is OpenRouter-only. This is the fix.Tests
15 new tests.
tests/test_cli/test_config.py:TestClassifyApiKey(3) andTestGetEffectiveByokKeys(6), includingtest_explicit_flag_beats_the_other_providers_ambient_key, which documents the billing hazard above.tests/test_cli/test_main.py:TestInitCommand(4) plus twoconfig settests.test_init_connection_test_uses_the_key_it_just_savedasserts on the outgoing header rather than merely that the health endpoint was called. That distinction matters: the old code stored an Anthropic key in the OpenRouter slot and then built its test client from that slot, so a "was it called" assertion passed while the request carried the key on a header the server ignores.Verified the tests are load-bearing by reverting
main.py's routing: 2 of the 4 init tests fail (assert None == 'sk-ant-api03-typed'andKeyError: 'X-Anthropic-API-Key').The one failure is the pre-existing
test_version_shows_version, which trips over Rich's ANSI color codes in the version string and fails identically on the epic branch.ruff checkandruff format --checkclean.Not covered by tests: the real outbound request to the dev deployment. That still needs the dev deploy to confirm the header is accepted end to end, and is tracked with the rest of the epic's deployment step.