#3413·GitNexus

wiki: add `anthropic` provider using the native `/v1/messages` endpoint

Author: enihcamCreated Sep 29, 2026Updated Sep 29, 2026

Area

gitnexus (CLI / core / indexing / MCP server)

Problem or opportunity

gitnexus wiki only talks to LLM APIs that speak the OpenAI Chat Completions protocol. The LLMProvider union in gitnexus/src/core/wiki/llm-client.ts lists openai | openrouter | azure | custom | cursor | claude | codex | opencode | grok | minimax, and callLLM() always builds POST <baseUrl>/chat/completions with Authorization: Bearer ….

There is no path to call the Anthropic Messages API directly with an ANTHROPIC_API_KEY. To use a Claude model for wiki generation today you must:

  1. Have the Claude Code CLI (claude) installed and logged in, then pass --provider claude. This spawns claude -p … as a child process and reads stdout. It does not work on machines that do not have Claude Code installed (CI, servers, shared hosts).
  2. Or go through OpenRouter with provider = custom. That works, but the request path, headers, and request body all pass through OpenRouter, which is not what the user asked for when they set an ANTHROPIC_API_KEY.

So a user with a plain Anthropic key, no Claude Code install, and no OpenRouter account cannot use gitnexus wiki. The local-CLI route in #1769 / #2039 covers the "already logged in to Claude Code" case but not the "I have an API key" case.

Proposed solution

Add a new provider value anthropic to LLMProvider. When selected, the wiki client should talk to the Anthropic Messages API directly:

  • URL: POST https://api.anthropic.com/v1/messages
  • Headers: x-api-key: <key>, anthropic-version: 2023-06-15, Content-Type: application/json
  • Auth source: --api-key flag, ANTHROPIC_API_KEY env var, or GITNEXUS_API_KEY
  • Model: --model <id> (e.g. claude-sonnet-4-5, claude-opus-4-1)
  • Optional base URL override for proxies (--base-url https://…) with the same validateLLMBaseUrl rules as other HTTP providers

The existing callLLM() in gitnexus/src/core/wiki/llm-client.ts should branch on config.provider === 'anthropic' and translate the OpenAI-style {role, content} messages into the Anthropic {system, messages} shape, then map the response back to LLMResponse. Streaming via SSE is already supported in readSSEStream; the Anthropic event names (message_start / content_block_delta / message_stop) need their own small parser or a translation layer.

Interactive gitnexus wiki setup in gitnexus/src/cli/wiki.ts should get a new menu entry for anthropic, mirroring the existing azure flow (prompt for API key, optional model, save to ~/.gitnexus/config.json).

Alternatives considered

  • provider = custom with base-url = https://api.anthropic.com/v1 and Authorization: Bearer …. Does not work today because callLLM() always appends /chat/completions and Anthropic does not serve that path. Would require either a base-URL rewrite or a provider = anthropic branch anyway.
  • provider = openrouter with model = anthropic/<id>. Works today at the cost of one more hop, but the user explicitly chose the Anthropic API.
  • provider = claude (local CLI from #1769). Already supported. Useful when Claude Code is installed and logged in; not a substitute for the bare API key path.
  • Add --anthropic-messages-endpoint flag to callLLM and call the Anthropic API from the existing OpenAI-compatible client. Rejected: mixes two wire formats inside one function and breaks the existing Azure / OpenRouter branches. A separate provider value is clearer.

Acceptance criteria

  • gitnexus wiki --provider anthropic --api-key <key> --model claude-sonnet-4-5 produces a wiki end-to-end against https://api.anthropic.com/v1/messages and writes the expected output files.
  • ANTHROPIC_API_KEY in the environment is picked up automatically when --provider anthropic is set and no --api-key is given.
  • gitnexus wiki --provider anthropic --base-url https://proxy.example/v1 works with the same validateLLMBaseUrl checks (https required, http only with allowlist) used for other HTTP providers.
  • Streaming output (onChunk callback in callLLM) still drives progress UI; the Anthropic SSE events are translated to delta.content chunks.
  • gitnexus wiki interactive setup has a new menu entry that saves provider: "anthropic" plus the model to ~/.gitnexus/config.json.
  • Unit tests cover: request URL + headers, message translation (system prompt into system, user content into messages), response translation, and the SSE event parser. npx tsc --noEmit and npm test pass.

Constraints

  • No new npm dependency. The Anthropic API is plain HTTPS + SSE; reuse the existing resilientFetch and readSSEStream plumbing.
  • The Anthropic API uses different finish reasons and error shapes from OpenAI. Map only what callLLM already consumes (choices[0].message.content); do not leak Anthropic-specific fields upward.
  • Do not change the existing openai / openrouter / azure / custom / claude / codex / opencode / cursor / grok / minimax provider behavior. This is a strictly additive change.
  • The claude local-CLI provider (CLI subprocess path in local-cli-client.ts) stays as is. claude (local CLI) and anthropic (HTTP API) are two distinct providers, similar to how cursor and custom coexist.

Contribution

  • I am willing to open a PR for this (may need design discussion first).

Source: abhigyanpatwari/GitNexus