Skip to content

feat(cli): accept an Anthropic key at every CLI entry point - #385

Merged
neuromechanist merged 3 commits into
feature/issue-360-epic-anthropic-platformfrom
feature/issue-384-cli-anthropic-byok
Sep 17, 2026
Merged

neuromechanist merged 3 commits into
feature/issue-360-epic-anthropic-platformfrom
feature/issue-384-cli-anthropic-byok

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Closes #384.

Problem

Phase 2 wired X-Anthropic-API-Key end to end, but no CLI entry point could put a key on it.

  • osa init prompted for "OpenRouter API key" and saved whatever it got into the OpenRouter slot. An Anthropic key typed there went out on X-OpenRouter-Key, where the server cannot use it.
  • osa config set had no --anthropic-key flag at all.
  • osa mirror built 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-key on ask and chat resolved into the OpenRouter slot regardless of the key's actual provider.

The only working Anthropic setup was to hand-edit credentials.yaml or export ANTHROPIC_API_KEY in every shell.

Change

The provider comes from the key's own prefix, mirroring the widget's inferKeyProvider:

  • classify_api_key(key) returns "anthropic" for sk-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-existing credentials.yaml.
  • get_effective_byok_keys(api_key) replaces get_effective_anthropic_key() and returns both slots. An explicit flag wins outright and clears the other slot: OSAClient prefers Anthropic whenever both are present, so without that an exported ANTHROPIC_API_KEY would 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.
  • init names 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 set gains --anthropic-key; the two key flags do not clobber each other.
  • ask, chat and mirror resolve both slots. --api-key help text across every command now names both providers.

Why this blocks the docs

cli-reference.md and getting-started.md in 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) and TestGetEffectiveByokKeys (6), including test_explicit_flag_beats_the_other_providers_ambient_key, which documents the billing hazard above. tests/test_cli/test_main.py: TestInitCommand (4) plus two config set tests.

test_init_connection_test_uses_the_key_it_just_saved asserts 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' and KeyError: 'X-Anthropic-API-Key').

uv run pytest tests/ -q -m "not llm and not integration and not network and not slow"
2021 passed, 13 skipped, 1 failed

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 check and ruff format --check clean.

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.

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.
@neuromechanist

Copy link
Copy Markdown
Member Author

All four review findings addressed in c7dab72.

  • _get_client had no test coverage. tests/test_cli/test_mirror.py did not exist. Five tests now cover it: Anthropic-only on a GET and on a POST, the OpenRouter path unchanged, -k sk-ant-... beating an exported OpenRouter key, and the no-key error path still reachable. They assert on the outgoing request, not on the returned client, because a client built with the right key and the wrong header name would pass the weaker check.
  • chat had no header test. Added, mirroring the ask one. Its response body is serialized from the server's own ChatResponse model rather than hand-written: the first attempt hand-wrote message as a string, but it is a nested ChatMessage and the CLI reads response["message"]["content"], so the test failed with AttributeError: 'str' object has no attribute 'get'. Building from the model means the fixture cannot drift from the contract.
  • Stale README. Quick Start now names both key sources, Configuration shows --anthropic-key alongside --openrouter-key, and the community section documents anthropic_api_key_env_var, which was missing entirely.
  • classify_api_key docstring overstated the parallel with the widget. Rewritten to state the difference: the widget validates the full key shape by regex and returns null for anything unrecognized, while the CLI routes unrecognized keys to OpenRouter so configs saved before the migration keep working. The reason is now in the docstring.

Verified all four new Anthropic-path tests fail when the mirror.py and main.py fixes are reverted. tests/test_cli/ is 127 passed, 1 pre-existing failure (test_version_shows_version, Rich ANSI codes, fails on the base branch too).

@neuromechanist
neuromechanist merged commit 84de2ed into feature/issue-360-epic-anthropic-platform Sep 17, 2026
3 of 4 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-384-cli-anthropic-byok branch September 17, 2026 15:55
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