Use Antseed with LangChain in Python
Drop-in ChatOpenAI(base_url=…) - works in chains, LCEL, and LangGraph agents.
What LangChain is. LangChain is the Python framework for composing LLMs with tools, retrievers, memory, and agents. The chat-model interface is BaseChatModel; ChatOpenAI from langchain-openai is a concrete subclass that talks the OpenAI Chat Completions wire format.
How Antseed plugs in. Pass your gateway URL as base_url and a gateway key as api_key to ChatOpenAI (on the same computer as a buyer, http://localhost:8377/v1 and any non-empty key work too). Once you have an instance, every primitive that accepts a chat model - LCEL pipes (prompt | llm | parser), tool-calling agents, create_react_agent, LangGraph nodes, RAG chains, structured-output binding via with_structured_output - will route through Antseed without any further changes.
One thing to know. LangChain's ChatOpenAI is OpenAI-strict by design: it will not preserve non-standard response fields like reasoning_content, reasoning, or reasoning_details that some third-party servers emit. For chat, tool-calling, and structured output this is fine. If you specifically need a model's reasoning traces, consider using the OpenAI Responses endpoint (/v1/responses) via a different provider package, or use a model that returns reasoning inline.
Run Antseed first
Every integration needs an Antseed endpoint: a gateway, best on a small server so every tool on every machine can reach it, or the local buyer proxy (same computer only). One-time setup, a few minutes.
Before you start
Prerequisites
- Python 3.10 or newer
Step 1
Install LangChain (Python)
- Install LangChain and the OpenAI integrationpip install -U langchain langchain-openai
Step 2
Point LangChain (Python) at Antseed
Recommended: a gateway
| ANTSEED_BASE_URL | https://gateway.example.com/v1 |
| ANTSEED_API_KEY | antseed_… |
Show as shell exports
Your gateway's base URL, ending in /v1, and an API key from the Keys page of its console. Recommended: a gateway on a server, which every machine can reach. A gateway on this computer is http://localhost:8379/v1; publish it with a tunnel (antseed tunnel start) for other machines. No gateway yet: see Run Antseed first above.
Same computer as a buyer, no gateway: base_url="http://localhost:8377/v1" and any non-empty api_key, such as "antseed"; the local proxy checks no key.
Step 3
Pick a model
Pick services whose protocols array includes openai-chat-completions (most do natively; the rest are translated automatically by @antseed/api-adapter). Tool calling and structured output rely on the service supporting OpenAI-style function-call syntax - confirm with a quick smoke test before building large agents. model="<peerId>@<service-id>" routes to a specific peer.
The full list is every model on the network. Run GET /v1/models on your endpoint, antseed network browse, or open the live network page to see what's available right now.
Verify
Test it
- Run the basic examplepython antseed_llm.pyExample outputHello! How can I help you today?
- Per-request peer override (no session pin needed)# extra_headers is forwarded as-is to Antseed. import os from langchain_openai import ChatOpenAI llm = ChatOpenAI( model="deepseek-v4-flash", base_url=os.environ["ANTSEED_BASE_URL"], api_key=os.environ["ANTSEED_API_KEY"], extra_headers={ "x-antseed-pin-peer": "cccccccccccccccccccccccccccccccccccccccc", }, ) print(llm.invoke("hi").content)
Use this when a single Python process needs to fan out to different peers per call (multi-tenant, scheduled jobs, A/B tests across peers).
- Verify it actually went through Antseedcurl -s "$ANTSEED_BASE_URL/key" -H "Authorization: Bearer $ANTSEED_API_KEY"
Shows the gateway key's usage so far; the console's Logs page lists each call with its seller and cost. With a local buyer,
antseed buyer meteringprints per-channel token + USDC totals instead.
How LangChain (Python) talks to Antseed
- Wire format sent by LangChain (Python):
OpenAI Chat Completions(hits/v1/chat/completionson your gateway or buyer proxy) - Best-fit services: any service whose
protocolsarray containsopenai-chat-completions. That's what the peer advertises as natively-supported - zero translation overhead, no transform edge cases. - How to check a peer: run
antseed network peer <peerId> --jsonand look atproviderServiceApiProtocols[provider].services[service]. The browse command exposes the same field per peer. - What happens when protocols don't match: Antseed's
@antseed/api-adaptertranslates between OpenAI Chat Completions and the service's native protocol on the fly, inside the buyer. So a request from LangChain (Python) can still reach a service that only advertisesanthropic-messages- just with a small transform step. - One known caveat: services whose only advertised protocol is
openai-responsesrequire streaming. If LangChain (Python) sends a non-streaming request and Antseed routes it to one of those services, the call fails withHTTP 400: Stream must be set to true. Pick a service whoseprotocolsincludesopenai-chat-completions(or another non-responses protocol) to avoid this.
If it goes wrong
Troubleshooting
openai.NotFoundError: 404 … model_not_foundNo seller your routing policy allows currently advertises the id you passed. Confirm withGET /v1/modelsand change themodel=argument.openai.AuthenticationError: 401ANTSEED_API_KEYis missing or not a valid gateway key. Check it against the Keys page in the console.openai.APIConnectionError: Connection refusedNothing answers atANTSEED_BASE_URL. Check the gateway is running and the URL ends in/v1. With the local buyer proxy, start it withantseed buyer start(or open the desktop app) and confirmcurl http://localhost:8377/v1/modelsworks.with_structured_outputreturns the right schema but empty fieldsEither the routed model does not support OpenAI tool-call syntax, or you usedmethod="json_mode"against a service that does not honor it. Trymethod="function_calling"(the default), and prefer services taggedcodingortoolsinantseed network peer <peerId> --json.- Streaming with
stream=Truetruncates mid-responseA buffering proxy (nginx, Cloudflare) sits between your code and Antseed. The gateway and the buyer proxy do not buffer SSE. Either bypass the intermediate proxy or set its buffering off (proxy_buffering off;in nginx). - Reasoning traces missing on a model you know emits themSee the third paragraph above:
langchain-openaidoes not preserve non-standard response fields. For first-class reasoning support, route the request through the OpenAI Responses endpoint (POST /v1/responses) using a Responses-aware client, or pick a model that puts reasoning inline incontent.
Reference
Links
Same category