feat: enable Bedrock fallback via LLM gateway#2097
Merged
Conversation
Inject the `x-posthog-use-bedrock-fallback: true` header on requests forwarded by the local auth proxy (which all Claude Agent SDK calls flow through) and on direct LlmGatewayService calls. The gateway falls back to Bedrock whenever Anthropic returns a 5xx. Generated-By: PostHog Code Task-Id: 95c00168-9e3e-4e16-b3d3-57118794df6b
Set `x-posthog-use-bedrock-fallback: true` via ANTHROPIC_CUSTOM_HEADERS in the Claude SDK subprocess env, so it applies to cloud runs (which reuse @posthog/agent) instead of only the desktop app's auth proxy. Generated-By: PostHog Code Task-Id: 95c00168-9e3e-4e16-b3d3-57118794df6b
Contributor
Prompt To Fix All With AIFix the following 2 code review issues. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 2
packages/agent/src/adapters/claude/session/options.ts:104-109
**Possible duplicate header injection**
If the operator's environment already contains `x-posthog-use-bedrock-fallback: true` in `ANTHROPIC_CUSTOM_HEADERS` (e.g. they've adopted the `posthog/wizard` approach at the process level), the header will appear twice in the child's env: once from the preserved existing value and once from the new append. Most gateways silently ignore duplicates, but the behaviour is technically undefined. A simple guard before appending would prevent this.
### Issue 2 of 2
packages/agent/src/adapters/claude/session/options.ts:104-120
**No tests for the new header-merging behaviour**
`options.test.ts` exercises agent registration and override but has nothing covering the `ANTHROPIC_CUSTOM_HEADERS` composition logic — neither the base case (env var absent → header set alone) nor the merge case (env var present → header appended). Given that the merge logic involves string manipulation that could silently produce wrong output (e.g. wrong separator, duplicate header), a pair of unit tests here would be cheap insurance and aligns with the team's preference for parameterised tests.
Reviews (1): Last reviewed commit: "refactor: move Bedrock fallback header t..." | Re-trigger Greptile |
charlesvien
approved these changes
May 7, 2026
ryans-posthog
approved these changes
May 7, 2026
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
x-posthog-use-bedrock-fallback: trueto requests sent through the LLM gateway so AWS Bedrock is used as a fallback whenever Anthropic returns a 5xx.AuthProxyService— covers every request the Claude Agent SDK subprocess makes (itsLLM_GATEWAY_URLpoints at this local proxy).LlmGatewayService— direct gateway calls (e.g. title generation).posthog/wizard, which sets the same header viaANTHROPIC_CUSTOM_HEADERS. Centralising injection in the proxy keeps it independent of Claude Agent SDK env-var support and applies uniformly to all forwarded traffic.Test plan
pnpm --filter code typecheckpassespnpm --filter code test— 1164 tests passCreated with PostHog Code