base_url.
This gives your LangChain app automatic retries, fallbacks, load balancing, caching, and guardrails — without changing your chain logic.
Installation
1
Start the gateway
- Local (npx)
- Portkey Cloud
http://localhost:8787/v1.2
Install dependencies
Basic setup
Setbase_url on ChatOpenAI to point at the gateway.
Adding routing configs via headers
Pass gateway configs and Portkey metadata throughdefault_headers (when constructing the client) or extra_headers (per-request).
createHeaders from portkey-ai is a convenience function that produces the correct x-portkey-* header dict. You can also construct headers manually.Using in a chain
Drop the configuredllm into any LangChain chain or pipeline as usual.