Skip to content

feat: enable Bedrock fallback via LLM gateway#2097

Merged
charlesvien merged 2 commits into
mainfrom
posthog-code/bedrock-fallback
May 7, 2026
Merged

feat: enable Bedrock fallback via LLM gateway#2097
charlesvien merged 2 commits into
mainfrom
posthog-code/bedrock-fallback

Conversation

@joshsny
Copy link
Copy Markdown
Contributor

@joshsny joshsny commented May 7, 2026

Summary

  • Adds x-posthog-use-bedrock-fallback: true to requests sent through the LLM gateway so AWS Bedrock is used as a fallback whenever Anthropic returns a 5xx.
  • Header is injected in two places:
    • AuthProxyService — covers every request the Claude Agent SDK subprocess makes (its LLM_GATEWAY_URL points at this local proxy).
    • LlmGatewayService — direct gateway calls (e.g. title generation).
  • Mirrors the approach in posthog/wizard, which sets the same header via ANTHROPIC_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 typecheck passes
  • pnpm --filter code test — 1164 tests pass
  • Smoke test live: trigger an Anthropic 5xx and confirm Bedrock fallback path engages on the gateway side

Created with PostHog Code

joshsny added 2 commits May 7, 2026 21:18
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
@joshsny joshsny marked this pull request as ready for review May 7, 2026 21:57
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 7, 2026

Prompt To Fix All With AI
Fix 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

Comment thread packages/agent/src/adapters/claude/session/options.ts
Comment thread packages/agent/src/adapters/claude/session/options.ts
@joshsny joshsny requested review from a team May 7, 2026 22:01
@charlesvien charlesvien merged commit 5846dd9 into main May 7, 2026
15 checks passed
@charlesvien charlesvien deleted the posthog-code/bedrock-fallback branch May 7, 2026 22:10
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.

3 participants