Use the Claude API with LangChain
The Claude API works with LangChain out of the box, and ChatAnthropic accepts a custom API URL — so your chains and agents can run on Claude through apiToken.sale after a two-line change. Same langchain-anthropic package, same model IDs, same streaming and tool calling; only the endpoint and the token price change.
·
Point ChatAnthropic at router.apitoken.sale
LangChain's Anthropic integration takes a custom API URL, so connecting the Claude API to LangChain through apiToken.sale is exactly two constructor arguments: anthropic_api_url and anthropic_api_key. Prompts, output parsers, callbacks and retry logic in your existing chains stay untouched.
from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(
model="claude-opus-4-8",
anthropic_api_url="https://router.apitoken.sale",
anthropic_api_key="sk-pool-•••",
)
print(llm.invoke("Hello").content)Pass the router root exactly as shown: no trailing slash, no /v1 suffix. The underlying Anthropic client appends /v1/messages itself, and a doubled path is the most common cause of a 404 on an otherwise correct setup.
One argument worth setting explicitly is max_tokens. ChatAnthropic defaults to 1024 output tokens, which silently truncates long answers — raise it for summarization or code-generation chains. Sampling parameters like temperature and top_p pass through unchanged, as do system prompts and stop sequences.
See also: Point the Anthropic SDK at apiToken.sale
Set it once with environment variables
If you share a codebase with people on the official endpoint — or run notebooks where editing source is awkward — skip the constructor arguments entirely. ChatAnthropic reads both values from the environment, so a checked-in project needs zero code changes.
export ANTHROPIC_API_URL=https://router.apitoken.sale export ANTHROPIC_API_KEY=sk-pool-•••
- 01Install the partner package: pip install -U langchain-anthropic. LangChain ships Anthropic support there, not in langchain-core.
- 02Generate a key in the apiToken.sale dashboard — it starts with sk-pool- and works across supported Claude, GPT, Gemini and Kimi models.
- 03Export ANTHROPIC_API_URL and ANTHROPIC_API_KEY as above (or put them in the .env file your runner loads).
- 04Instantiate ChatAnthropic(model="claude-sonnet-5") with no other arguments and run one invoke() to confirm a normal response.
Explicit constructor arguments win over environment variables, so a local override never leaks into the shared configuration. The env approach also keeps the key out of git history — treat sk-pool-… like any other secret: .env files stay uncommitted and CI gets the value from its secret store.
Streaming, tool calling and LangGraph stay intact
The gateway serves the standard Anthropic Messages API, and LangChain talks to it through the official client. Everything built on that protocol — SSE streaming, tool-use blocks, structured output — behaves exactly as it does against api.anthropic.com. That includes with_structured_output(), which LangChain implements on top of tool calling, and .astream_events() for token-level callbacks in async apps.
from langchain_anthropic import ChatAnthropic
from langchain_core.tools import tool
@tool
def get_weather(city: str) -> str:
"""Return the current weather for a city."""
return f"Sunny in {city}"
llm = ChatAnthropic(model="claude-sonnet-5") # env vars supply URL and key
llm_with_tools = llm.bind_tools([get_weather])
for chunk in llm_with_tools.stream("What is the weather in Paris?"):
print(chunk.content, end="")LangGraph agents inherit the same setup, because a graph node just invokes a chat model. Point the model at the router once and every agent, supervisor and sub-graph built on it follows — there is no LangGraph-specific configuration to redo.
Token accounting keeps working too: each AIMessage still carries usage_metadata with input and output token counts, because the gateway returns the standard Anthropic usage object. LangSmith traces and custom callbacks that read usage_metadata need no changes.
What changes and what does not
Before migrating a production app, it helps to see the full delta in one place. The short version: your code, your models and your LangChain features stay put — the endpoint, the key and the per-token price are the only moving parts.
| Concern | Through apiToken.sale |
|---|---|
| Model IDs | Unchanged — claude-opus-4-8, claude-sonnet-5, claude-haiku-4-5 and the rest of the catalog |
| Protocol | Unchanged — Anthropic Messages API via the official client |
| Streaming and tool calling | Unchanged — SSE chunks and tool-use blocks as usual |
| Chains, agents, LangGraph | Unchanged — no code edits beyond URL and key |
| Price per token | 50% less on the same models |
| API key | One sk-pool-… key for supported Claude, GPT, Gemini and Kimi models |
| Billing | Prepaid balance with per-key spend and token detail in the dashboard |
Pick the right Claude model for each node
Because switching models is a one-argument change, treat model choice as a per-node decision instead of a global one. A router chain that classifies intent does not need the same tier as the node that writes the final answer.
- claude-haiku-4-5 — the fast, inexpensive tier: classification, routing, extraction and other high-volume steps.
- claude-sonnet-5 — the balanced default for most production chains, RAG pipelines and coding agents.
- claude-opus-4-8 — the top reasoning tier; reserve it for hard analysis, long documents and agent planning steps.
from langchain_anthropic import ChatAnthropic fast = ChatAnthropic(model="claude-haiku-4-5") # routing, extraction balanced = ChatAnthropic(model="claude-sonnet-5") # default nodes deep = ChatAnthropic(model="claude-opus-4-8") # planning, hard analysis router_chain = router_prompt | fast answer_chain = answer_prompt | balanced | StrOutputParser()
All three instances share the same environment-supplied URL and key, and every call bills to one prepaid balance. That makes tier experiments cheap: swap the model string, rerun your evaluation set, keep what wins.
Prototype on Sonnet, then downgrade the simple nodes to Haiku and escalate only the hard ones to Opus. With prepaid per-token billing, a mixed-tier chain costs noticeably less than running everything on the flagship.
Troubleshooting the connection
Only the endpoint and the key change, so almost every failure is one of three configuration mistakes — not a LangChain problem. Work through them in order before touching chain code.
- 401 Unauthorized — the key is missing or mistyped, or the environment variable never reached the process. Print os.environ in the same interpreter to confirm, and remember constructor arguments override env vars.
- 404 Not Found — the URL carries an extra /v1 or a trailing path. Use the bare router root https://router.apitoken.sale.
- Model not found — re-check the ID against the catalog at /models; the IDs here are the same ones Anthropic publishes.
If you are unsure whether the gateway or your chain is at fault, swap the URL back to the official endpoint for one run. Identical behavior means the bug is in the chain; a difference narrows it to configuration.
For transient 429 or 5xx responses you do not need custom logic: ChatAnthropic retries twice with backoff by default (tune with max_retries). Long-running agents should still set an explicit timeout in seconds rather than relying on the client default.
Frequently asked questions
Does LangChain work with a custom Claude API endpoint?
Yes. ChatAnthropic accepts anthropic_api_url (or the ANTHROPIC_API_URL environment variable), so you can point it at https://router.apitoken.sale and keep everything else — package, model IDs, chain code — unchanged.
How do I set the LangChain Anthropic base URL without changing code?
Export ANTHROPIC_API_URL=https://router.apitoken.sale and ANTHROPIC_API_KEY=sk-pool-… before running your script. ChatAnthropic picks both up automatically, so shared repositories need no edits at all.
Do streaming and tool calling still work through apiToken.sale?
Yes. The gateway serves the standard Anthropic Messages API, so .stream(), bind_tools(), structured output and LangGraph agents behave exactly as with the official endpoint.
Which Claude models can I call from LangChain?
All supported Claude models — claude-opus-4-8, claude-sonnet-5, claude-haiku-4-5 and more — on the same key and prepaid balance, at 50% less per token.
Can I use ChatOpenAI instead of ChatAnthropic for Claude?
Yes. The router also exposes an OpenAI-compatible lane at https://router.apitoken.sale/v1, so ChatOpenAI(base_url="https://router.apitoken.sale/v1", api_key="sk-pool-•••") reaches the same Claude models with the same key — handy when a framework only speaks the OpenAI protocol.
Do I need a separate key for GPT, Gemini or Kimi in LangChain?
No. The same sk-pool-… key works across supported Claude, GPT, Gemini and Kimi models, so a multi-provider LangChain app can share one key and one prepaid balance.
Try it before you pay: new Google/GitHub accounts include $5 of platform bonus credit.