Skip to content

Add --anthropic-path-prefix flag and auto-probe for Envoy AI Gateway#5174

Open
yrobla wants to merge 1 commit intomainfrom
feat/issue-5158
Open

Add --anthropic-path-prefix flag and auto-probe for Envoy AI Gateway#5174
yrobla wants to merge 1 commit intomainfrom
feat/issue-5158

Conversation

@yrobla
Copy link
Copy Markdown
Contributor

@yrobla yrobla commented May 4, 2026

Summary

Some gateways (e.g. Envoy AI Gateway) serve native-Anthropic traffic at /anthropic/v1/messages rather than /v1/messages, causing Claude Code to receive 404s and report "model not found" errors.

  • Add --anthropic-path-prefix flag to thv llm setup (default empty). When set, the prefix is appended to the gateway URL when writing ANTHROPIC_BASE_URL in direct-mode tool configs (Claude Code, Codex).
  • Auto-probe: if the flag is omitted, issue a HEAD request to /anthropic/v1/messages; a 401 response (auth-reachable path) triggers automatic prefix selection. 404 or any error means no prefix.
  • Proxy-mode tools (Cursor, VS Code) are unaffected — they route through thv llm proxy and don't use ANTHROPIC_BASE_URL.

Fixes #5158

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

  • Unit tests (task test)
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

API Compatibility

  • This PR does not break the v1beta1 API, OR the api-break-allowed label is applied and the migration guidance is described above.

Changes

File Change

Does this introduce a user-facing change?

Implementation plan

Approved implementation plan

Special notes for reviewers

@yrobla yrobla requested a review from Copilot May 4, 2026 07:18
@github-actions github-actions Bot added the size/S Small PR: 100-299 lines changed label May 4, 2026
@yrobla yrobla force-pushed the feat/issue-5158 branch from 91876a7 to 2a39040 Compare May 4, 2026 07:23
@github-actions github-actions Bot added size/S Small PR: 100-299 lines changed and removed size/S Small PR: 100-299 lines changed labels May 4, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds support in thv llm setup for gateways (notably Envoy AI Gateway) that serve Anthropic-native traffic under an /anthropic path prefix, preventing 404 “model not found” errors for direct-mode tools that rely on ANTHROPIC_BASE_URL.

Changes:

  • Introduces --anthropic-path-prefix and propagates an effective AnthropicBaseURL into direct-mode tool config patching.
  • Adds an auto-probe that HEADs <gateway>/anthropic/v1/messages and selects /anthropic when a 401 indicates the route exists.
  • Updates Claude Code integration to write ANTHROPIC_BASE_URL from AnthropicBaseURL (with fallback to GatewayURL) and adds unit tests for prefix behavior + probing.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
pkg/llmgateway/config.go Extends ApplyConfig to carry an AnthropicBaseURL alongside the gateway URL.
pkg/llm/setup.go Resolves/passes the Anthropic prefix, applies it only for direct-mode tools, and implements the auto-probe.
pkg/llm/setup_test.go Adds unit tests for direct/proxy prefix behavior and probe outcomes.
pkg/llm/manage.go Extends SetOptions with AnthropicPathPrefix.
pkg/client/llm_gateway.go Adds AnthropicBaseURL value resolution with fallback to GatewayURL.
pkg/client/config.go Switches Claude Code’s ANTHROPIC_BASE_URL mapping to AnthropicBaseURL.
cmd/thv/app/llm.go Adds the --anthropic-path-prefix CLI flag to thv llm setup.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/llm/manage.go Outdated
Comment thread pkg/client/config.go
Comment thread pkg/llm/setup.go
Comment thread pkg/llm/setup.go Outdated
@yrobla yrobla force-pushed the feat/issue-5158 branch from 2a39040 to ec8c2ae Compare May 4, 2026 07:44
@github-actions github-actions Bot added size/S Small PR: 100-299 lines changed and removed size/S Small PR: 100-299 lines changed labels May 4, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 4, 2026

Codecov Report

❌ Patch coverage is 63.33333% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.57%. Comparing base (9057f7a) to head (965c18c).

Files with missing lines Patch % Lines
pkg/llm/setup.go 64.28% 14 Missing and 6 partials ⚠️
pkg/client/llm_gateway.go 50.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5174      +/-   ##
==========================================
- Coverage   67.57%   67.57%   -0.01%     
==========================================
  Files         602      602              
  Lines       61119    61167      +48     
==========================================
+ Hits        41303    41334      +31     
- Misses      16693    16704      +11     
- Partials     3123     3129       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/llm/setup.go Outdated
Comment thread pkg/llm/setup.go Outdated
Comment thread cmd/thv/app/llm.go
@yrobla yrobla force-pushed the feat/issue-5158 branch from ec8c2ae to 1bf6e71 Compare May 4, 2026 11:26
@github-actions github-actions Bot added size/M Medium PR: 300-599 lines changed and removed size/S Small PR: 100-299 lines changed labels May 4, 2026
@yrobla yrobla requested a review from Copilot May 4, 2026 13:16
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

cmd/thv/app/llm.go:233

  • PR description mentions Codex being affected (“direct-mode tool configs (Claude Code, Codex)”), but Codex doesn’t appear to have LLM gateway support in supportedClientIntegrations (it has no LLMGatewayMode / LLMGatewayKeys). If Codex is intentionally out of scope, please update the PR description to avoid confusing users; if it should be in scope, it likely needs an LLM gateway config entry similar to other direct-mode tools.
	cmd := &cobra.Command{
		Use:   "setup",
		Short: "Configure detected AI tools to use the LLM gateway",
		Long: `Detect installed AI coding tools (Claude Code, Gemini CLI, Cursor, VS Code,
Xcode) and patch each tool's configuration to route through the LLM gateway.

Token-helper tools (Claude Code, Gemini CLI) are configured to call
"thv llm token" to obtain a fresh OIDC token on demand.

Proxy-mode tools (Cursor, VS Code, Xcode) are configured to send requests to
the localhost reverse proxy started by "thv llm proxy start".

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/llm/setup.go Outdated
@yrobla yrobla force-pushed the feat/issue-5158 branch from 1bf6e71 to 4f0e3cd Compare May 4, 2026 14:27
@github-actions github-actions Bot added size/M Medium PR: 300-599 lines changed and removed size/M Medium PR: 300-599 lines changed labels May 4, 2026
@yrobla yrobla force-pushed the feat/issue-5158 branch from 4f0e3cd to c3ebe29 Compare May 4, 2026 14:38
@github-actions github-actions Bot added size/M Medium PR: 300-599 lines changed and removed size/M Medium PR: 300-599 lines changed labels May 4, 2026
Some gateways (e.g. Envoy AI Gateway) serve native-Anthropic traffic at
/anthropic/v1/messages rather than /v1/messages, causing Claude Code to
receive 404s and report "model not found" errors.

- Add --anthropic-path-prefix flag to `thv llm setup` (default empty).
  When set, the prefix is appended to the gateway URL when writing
  ANTHROPIC_BASE_URL in direct-mode tool configs (Claude Code, Codex).
- Auto-probe: if the flag is omitted, issue a HEAD request to
  <gateway>/anthropic/v1/messages; a 401 response (auth-reachable path)
  triggers automatic prefix selection. 404 or any error means no prefix.
- Proxy-mode tools (Cursor, VS Code) are unaffected — they route through
  `thv llm proxy` and don't use ANTHROPIC_BASE_URL.

Closes #5158
@yrobla yrobla force-pushed the feat/issue-5158 branch from c3ebe29 to 965c18c Compare May 4, 2026 14:51
@yrobla yrobla requested a review from Copilot May 4, 2026 14:52
@github-actions github-actions Bot added size/M Medium PR: 300-599 lines changed and removed size/M Medium PR: 300-599 lines changed labels May 4, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cmd/thv/app/llm.go
Comment thread pkg/client/llm_gateway.go
Comment on lines +293 to +299
case "AnthropicBaseURL":
// Use the pre-computed Anthropic base URL when available; fall back to
// GatewayURL so existing configs continue to work without the prefix.
if cfg.AnthropicBaseURL != "" {
return cfg.AnthropicBaseURL, nil
}
return cfg.GatewayURL, nil
Comment thread pkg/llmgateway/config.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M Medium PR: 300-599 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

thv llm setup doesn't append provider path prefix for Envoy AI Gateway

4 participants