calling-the-ai-gateway
Skill733 starsChanged 6 days agoMITView on GitHub
Adds or changes a monorepo caller of the Go AI gateway (PostHog/ai-gateway). Use when adding an LLM call from Django, a Temporal or Celery worker, or a sandbox agent; when changing a caller's gateway credential, base URL, headers, attribution, or model; or when adding a task profile whose sandbox runs must mint a gateway token. Covers the `posthog/llm/gateway_client.py` builders, `phs_`/`pha_` credentials with `llm_gateway:read`, the `X-PostHog-Properties` attribution header, `SANDBOX_AI_GATEWAY_*` settings, and the `BACKGROUND_AGENTS_ORIGINS` and `MINTABLE_PRODUCTS` token gates.
- Reads credentials
1 other project uses a version of this.
Adopted by
Most recently taken up by
- PostHog/posthogword for word, first seen Nov 29, 2026
Other versions1 project uses a version of this.
Word for word · 1
- PostHog/posthog★ 41k100% same
Edited · 0
None found.
Wrote their own version · 0
None found.
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository131 other files it gives its agents.
- AGENTS.mdAGENTS.md
- common/AGENTS.mdAGENTS.md
- hog/AGENTS.mdAGENTS.md
- proto/AGENTS.mdAGENTS.md
- rust/AGENTS.mdAGENTS.md
- .cursor/rules/django-python.mdcCursor rule
- .cursor/rules/react-typescript.mdcCursor rule
- .cursor/rules/rust.mdcCursor rule
- adding-activity-loggingSkill
- adding-api-scopesSkill
- adding-dashboard-v2-custom-contentSkill
- adding-dashboard-v2-panel-typesSkill
- adding-inbound-webhooksSkill
- adding-inbox-sourcesSkill
- adding-ingestion-warningsSkill
- adding-mcp-store-serversSkill
- adding-personhog-rpcSkill
- adding-product-alertingSkill
- adding-project-secret-api-key-authSkill
- adding-today-highlightsSkill
- adopting-generated-api-typesSkill
- analyzing-experiment-precompute-canarySkill
- analyzing-experiment-query-performanceSkill
- analyzing-insights-across-teamsSkill
- announcing-behavior-changesSkill
- auditing-warehouse-source-coverageSkill
- authenticating-to-clickhouseSkill
- authoring-ci-workflowsSkill
- building-on-capture-v1Skill
- building-product-empty-statesSkill
- building-the-toolbarSkill
- clickhouse-migrationsSkill
- creating-customer-analytics-account-widgetsSkill
- debugging-ci-failuresSkill
- debugging-local-replaySkill
- debugging-local-task-agent-runsSkill
- debugging-mcp-analyticsSkill
- debugging-signals-pipelineSkill
- debugging-table-access-deniedSkill
- depot-ciSkill
- depot-container-buildsSkill
- depot-github-runnersSkill
- developing-subscriptionsSkill
- developing-the-cliSkill
- django-migrationsSkill
- django-startup-timeSkill
- documenting-warehouse-sourcesSkill
- editing-agents-mdSkill
- establishing-code-ownershipSkill
- exporting-recordings-to-customer-s3Skill
- extending-hobby-smoke-testsSkill
- extending-personhog-test-harnessSkill
- extending-the-admin-consoleSkill
- fixing-flaky-testsSkill
- formatting-slack-messagesSkill
- gating-production-deploysSkill
- gating-sensitive-actionsSkill
- generating-clickhouse-query-performance-reportsSkill
- hogliSkill
- implementing-mcp-toolsSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
6 rules. About 0 are common, 1 is distinctive, and 5 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 28
Use the slugless base URL from AI_GATEWAY_URL (it ends in /v1).
- Very specific: line 31
Treat ai_product as telemetry.
- Very specific: line 32
Check the model and API shape against the gateway's /v1/models catalog.
- Very specific: line 34
If the process has no gateway credential, enable the ai-gateway feature flag for the paying team, create a phs_ project secret with …
- Distinctive: line 35
Check with the AI gateway team before adding deployment variables or secrets, so existing shared configuration can be reused.
- Very specific: line 60
Run the narrow caller tests and the formatter or type checker for touched code.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Calling the AI gateway
Gateway behavior (auth, billing, routing, providers, models) lives in PostHog/ai-gateway.
A change to what the gateway does lands there. This monorepo only calls it.
Inventory the caller
Find the call site, client construction, settings, deployment wiring, and tests. Record:
- the use case and the
ai_productit reports as - the credential source and the team whose wallet pays
- API shape (OpenAI or Anthropic), model, streaming, and tool or structured-output needs
- distinct ID, trace ID, session ID, and custom property attribution
- timeout, retry, and error handling
Build the client
- For Django and worker code, use a builder from
posthog/llm/gateway_client.py.build_openai_client,build_async_openai_client,build_anthropic_clientandbuild_async_anthropic_clientauthenticate with theAI_GATEWAY_API_KEYproject secret.build_ai_gateway_openai_clientsends the caller's ownpha_orphe_bearer;build_anthropic_clientalso takesprivacy_mode. Each raisesGatewayNotConfiguredErrorwhen the gateway is not configured; handle it where the caller can degrade. - Use the slugless base URL from
AI_GATEWAY_URL(it ends in/v1). The Anthropic builders strip/v1because the SDK appends/v1/messages. - Authenticate with a
phs_orpha_credential that carriesllm_gateway:read. Do not expose a shared secret to an untrusted runtime. - Send event labels in one
X-PostHog-PropertiesJSON object, through the builder'spropertiesargument orai_gateway_headers. Use the dedicated distinct ID and trace ID arguments where the caller has them. - Treat
ai_productas telemetry. It does not grant product auth or billing policy. - Check the model and API shape against the gateway's
/v1/modelscatalog.
If the process has no gateway credential, enable the ai-gateway feature flag for the paying team, create a phs_ project secret with llm_gateway:read, and wire it through the existing deployment secret mechanism.
Check with the AI gateway team before adding deployment variables or secrets, so existing shared configuration can be reused.
Sandbox callers
The sandbox reaches the gateway at SANDBOX_AI_GATEWAY_URL, and only for products listed in SANDBOX_AI_GATEWAY_PRODUCTS.
docs/internal/sandboxes-setup-guide.md describes those settings and the token caps.
A sandbox run reaches the gateway only when the tasks worker mints it a token.
A new task profile with billing_product="background_agents" must add its origin to BACKGROUND_AGENTS_ORIGINS in products/tasks/backend/temporal/process_task/ai_gateway_token.py and to Task.SERVER_ONLY_ORIGIN_PRODUCTS.
A missing origin is refused with no_background_agents_provenance, and the tasks.gateway_token.refused metric carries the reason.
test_server_funded_profile_can_mint_a_gateway_token in products/tasks/backend/tests/test_profile_parity.py fails until both entries exist.
A profile with a dedicated gateway product, such as account_audit or autoresearch, instead needs the product in MINTABLE_PRODUCTS, a provenance check in mint_refusal, and an entry in GATEWAY_PRODUCTS in packages/agent/packages/agent/src/utils/gateway.ts.
Tests
Invoke /writing-tests before changing tests. Cover the contract at the lowest useful level:
- the selected base URL
- the credential and required headers, without asserting secret values
- the
X-PostHog-PropertiesJSON and trace attribution - the model and API shape
- the failure behavior the caller handles, including
GatewayNotConfiguredError
Verify
Run the narrow caller tests and the formatter or type checker for touched code.
Exercise a real request against a local gateway (hogli start with the ai_gateway capability, or bin/setup-gateway-e2e) when credentials are available, and confirm the generation's attribution.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

