How to Integrate Gate.AI with LangChain and LangGraph
Gate.AI LangChain & LangGraph Integration Guide
Gate.AI provides an OpenAI-compatible API endpoint that developers can use with LangChain and LangGraph to run model calls through Gate.AI routing. This matters when a Python application needs chain-based prompts, graph-based agent workflows, or a shared model gateway without rewriting application logic for each model provider. This guide covers local setup, a LangChain test call, a LangChain prompt chain, and a simple LangGraph workflow; it does not cover production deployment, vector databases, observability, billing configuration, or enterprise access policies.
Prerequisites
- A Gate.AI API key created from your Gate.AI account.
- Python 3.10 or later with permission to install packages.
Source basis: Gate.AI official documentation and product material, as of June 2026.
What Will You Be Able to Do After This Guide?
You will be able to connect Gate.AI to LangChain through ChatOpenAI, then reuse the same model configuration inside a LangGraph workflow.
This setup helps you:
- call Gate.AI from a local Python script
- test
model="auto"for Gate.AI routing - replace
autowith a verified Gate.AI model ID when needed - run a LangChain prompt chain
- run a two-step LangGraph workflow
For broader API setup context, see Gate.AI API integration for developers.
Step 1: Install the Python Packages
This step installs the LangChain OpenAI integration and LangGraph package required for the local workflow.
Create and activate a virtual environment:
python -m venv .venvsource .venv/bin/activatepip install -U langchain langchain-openai langgraph
For Windows PowerShell, activate with:
.venv\Scripts\Activate.ps1
You should be able to import langchain_openai and langgraph after installation.
Step 2: Store the Gate.AI API Key
This step keeps the Gate.AI API key outside your source code.
Set environment variable in bash:
export GATEAI_API_KEY="YOUR_API_KEY"
For Windows PowerShell:
setx GATEAI_API_KEY "YOUR_API_KEY"
Restart the PowerShell session after using setx.
Do not commit real API keys to Git. For team projects, use your secret manager, CI secret settings, or approved internal environment variable workflow.
Step 3: Configure Gate.AI in LangChain
This step creates a LangChain chat model that sends OpenAI-compatible requests to Gate.AI.
According to Gate.AI documentation as of June 2026, the OpenAI-compatible Base URL is:
https://api.gate.ai/openai/v1
Use this value as the
base_urlin LangChain. Do not add/chat/completionsto thebase_url, because LangChain handles the chat completion path internally.Example:
import osfrom langchain_openai import ChatOpenAIllm = ChatOpenAI(model="auto",api_key=os.environ["GATEAI_API_KEY"],base_url="https://api.gate.ai/openai/v1",temperature=0,)response = llm.invoke("Write one sentence explaining what an AI model router does.")print(response.content)
Expected result:
An AI model router sends a request to an appropriate model based on the task, routing rules, or configuration.
The exact wording may differ because the response depends on the model selected by Gate.AI routing.
Step 4: Build a LangChain Prompt Chain
This step connects a reusable prompt, the Gate.AI-backed model, and a string output parser.
Example:
import osfrom langchain_openai import ChatOpenAIfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.output_parsers import StrOutputParserllm = ChatOpenAI(model="auto",api_key=os.environ["GATEAI_API_KEY"],base_url="https://api.gate.ai/openai/v1",temperature=0,)prompt = ChatPromptTemplate.from_messages([("system", "You are a concise technical assistant."),("human", "Explain {topic} in three bullet points."),])chain = prompt | llm | StrOutputParser()result = chain.invoke({"topic": "Gate.AI API routing"})print(result)
You should see a short explanation in three bullet points. If the script fails before returning text, check the API key, Base URL, and model value before changing the chain structure.
Step 5: Configure Gate.AI in LangGraph
This step reuses the same Gate.AI model configuration inside a LangGraph state workflow.
The example below uses one node to draft a short explanation and another node to review the draft. This keeps the workflow simple enough to verify before adding tools, memory, retrieval, or conditional routing.
Example:
import osfrom typing_extensions import TypedDictfrom langchain_openai import ChatOpenAIfrom langgraph.graph import StateGraph, START, ENDllm = ChatOpenAI(model="auto",api_key=os.environ["GATEAI_API_KEY"],base_url="https://api.gate.ai/openai/v1",temperature=0,)class WorkflowState(TypedDict):topic: strdraft: strreview: strdef draft_node(state: WorkflowState) -> dict:response = llm.invoke([("system", "You write short technical explanations."),("human", f"Write a two-sentence explanation of {state['topic']}."),])return {"draft": response.content}def review_node(state: WorkflowState) -> dict:response = llm.invoke([("system", "You review technical writing for clarity."),("human", f"Review this draft and suggest one improvement:\n\n{state['draft']}"),])return {"review": response.content}builder = StateGraph(WorkflowState)builder.add_node("draft", draft_node)builder.add_node("review", review_node)builder.add_edge(START, "draft")builder.add_edge("draft", "review")builder.add_edge("review", END)app = builder.compile()result = app.invoke({"topic": "Gate.AI with LangGraph"})print("Draft:\n", result["draft"])print("\nReview:\n", result["review"])
You should see both a generated draft and a review comment. If the workflow returns only the draft, confirm that the edge from draft to review is present.
Step 6: Replace Auto Routing with a Specific Model
This step makes the integration more predictable when you need fixed model behavior.
- Use
model="auto"for initial routing tests when Gate.AI auto routing is enabled and supported for your account. Use a specific Gate.AI model ID when you need repeatable behavior, evaluation consistency, latency testing, or production review.
Example:
llm = ChatOpenAI(model="YOUR_MODEL_ID",api_key=os.environ["GATEAI_API_KEY"],base_url="https://api.gate.ai/openai/v1",temperature=0,)
Copy the model ID from the Gate.AI model catalog or your Gate.AI Console. Do not guess the model ID, because availability may vary by account, product status, and model provider rules as of June 2026.
Which Configuration Values Matter Most?
| Setting | Example Value | Where It Is Used | Why It Matters |
|---|---|---|---|
| API key variable | GATEAI_API_KEY | Shell and Python code | Keeps credentials out of source files |
| Base URL | https://api.gate.ai/openai/v1 | ChatOpenAI(base_url=…) | Routes OpenAI-compatible calls to Gate.AI |
| Model | auto or YOUR_MODEL_ID | ChatOpenAI(model=…) | Chooses routing or a fixed model |
| Temperature | 0 | ChatOpenAI(temperature=0) | Reduces output variation during setup testing |
Use one shared llm object across LangChain and LangGraph when you want consistent routing behavior. Change only the model value when moving from a routing test to a fixed-model test.
Why Is the Gate.AI LangChain LangGraph Integration Not Working? Troubleshooting Checklist
Symptom: The request returns 401, invalid_api_key, or an authentication error.
- Cause: The Gate.AI API key is missing, expired, mistyped, or unavailable in the current shell.
- Fix: Run
echo $GATEAI_API_KEYin the same terminal session, confirm the key exists in Gate.AI, and restart the terminal if you set the variable in another session.
Symptom: The request returns 404, connection failure, or endpoint not found.
- Cause: The Base URL is incorrect. The correct OpenAI-compatible Base URL is
https://api.gate.ai/openai/v1. - Fix: Set
base_url="https://api.gate.ai/openai/v1"in everyChatOpenAIinstance.
Symptom: Python returns ModuleNotFoundError.
- Cause: The current virtual environment does not contain
langchain-openaiorlanggraph. - Fix: Activate the virtual environment and run
pip install -U langchain langchain-openai langgraph.
Symptom: Authentication succeeds, but the model request fails.
- Cause: The selected model is unavailable, misspelled, or unsupported for the request.
- Fix: Test with
model="auto"first. For fixed-model tests, copy a verified model ID from Gate.AI.
Symptom: The LangGraph workflow returns an incomplete state.
- Cause: A node did not return the expected state key, or a graph edge is missing.
- Fix: Confirm that each node returns a dictionary with the expected key, then confirm the graph includes
START, every node edge, andEND.
What Can You Configure or Build Next?
- Use Gate.AI API integration for developers to connect this workflow to a broader Gate.AI API setup.
- Use Gate.AI Cursor setup if you want to use Gate.AI inside an AI coding editor workflow.
- Use Gate.AI Claude Code setup if your developer workflow uses Claude Code and an Anthropic-compatible setup.
FAQs
Can I use the same Gate.AI configuration for LangChain and LangGraph?
Yes. Create one ChatOpenAI object with the Gate.AI API key, Gate.AI Base URL, and selected model. Reuse that object in a LangChain chain or inside LangGraph node functions.
Should I use auto or a specific model ID?
Use auto for initial routing tests when Gate.AI auto routing is enabled. Use a specific Gate.AI model ID when you need repeatable model behavior, controlled evaluations, or production review.
Why should the Base URL include /openai/v1?
Gate.AI uses https://api.gate.ai/openai/v1 for OpenAI-compatible requests. LangChain’s ChatOpenAI should point to that Base URL, not to the shorter /v1 path.
Does this setup require changing LangGraph itself?
No. LangGraph calls the model object inside node functions. The Gate.AI-specific configuration stays in the ChatOpenAI setup.


