How to Integrate Gate.AI with LangChain and LangGraph
Gate.AI provides OpenAI-compatible API endpoints. Developers can use these endpoints to make model calls routed through Gate.AI, combined with LangChain and LangGraph. This approach is especially useful when your Python application needs chain-based prompts, graph-based agent workflows, or you want to build a unified model gateway so you don’t have to rewrite application logic for every model provider. This article walks you through setting up your local environment, testing calls with LangChain, building a LangChain prompt chain, and creating a simple LangGraph workflow. It does not cover production deployment, vector databases, observability, billing configuration, or enterprise access policies.
Prerequisites
- You have created a Gate.AI API key via your Gate.AI account.
- Python 3.10 or higher, with permissions to install dependencies.
Source: Gate.AI official documentation and product materials, as of June 2026.
What capabilities will you have after completing this guide?
You’ll be able to connect Gate.AI to LangChain through ChatOpenAI, and reuse the same model configuration inside a LangGraph workflow.
This solution helps you:
- Call Gate.AI in a local Python script
- Test the routed configuration
model="auto" - Replace
autowith a verified Gate.AI model ID when needed - Run a LangChain prompt chain
- Run a two-step LangGraph workflow
For broader API integration context, see Gate.AI Developer API Integration.
Step 1: Install Python dependencies
In this step, you’ll install the LangChain OpenAI integration and the LangGraph packages needed for the local workflow.
Create and activate a virtual environment:
python -m venv .venvsource .venv/bin/activatepip install -U langchain langchain-openai langgraph
Activate command in Windows PowerShell:
.venv\Scripts\Activate.ps1
After installation, you should be able to import langchain_openai and langgraph normally.
Step 2: Store your Gate.AI API key
In this step, you’ll store the Gate.AI API key outside your source code.
Set an environment variable in a bash environment:
export GATEAI_API_KEY="YOUR_API_KEY"
Set in Windows PowerShell:
setx GATEAI_API_KEY "YOUR_API_KEY"
After using setx, you need to restart your PowerShell session.
Do not commit your real API key to Git. For team projects, use a secrets manager, CI secret configuration, or an approved internal environment-variable workflow.
Step 3: Configure Gate.AI in LangChain
In this step, you’ll create a chat model in LangChain that sends requests using the OpenAI-compatible protocol to Gate.AI.
According to the Gate.AI documentation (June 2026), the OpenAI-compatible Base URL is:
https://api.gate.ai/openai/v1
In LangChain, set this address as
base_url. You do not need to append/chat/completionsafterbase_url; LangChain will handle the path automatically.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 output:
An AI model router distributes requests to the right model based on the task, routing rules, or configuration.
The actual returned content may vary, because Gate.AI routing responds dynamically based on the selected model.
Step 4: Build a LangChain prompt chain
In this step, you’ll connect reusable prompts, the Gate.AI-supported 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="XX/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 concise explanation in three bullet points. If your script throws an error before returning text, check your API key, Base URL, and model configuration first—don’t directly modify the chain structure.
Step 5: Configure Gate.AI in LangGraph
In this step, you’ll reuse the same Gate.AI model configuration in a LangGraph state workflow.
The example below uses one node to generate a short draft and another node to review it. This keeps the flow simple, so you can validate the basics before adding tools, memory, retrieval, or conditional routing.
Example:
```python
import os
from typing_extensions import TypedDict
from langchain_openai import ChatOpenAI
from 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: str
def 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:
{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:
", result["draft"])
print("
Review:
", result["review"])
You’ll see the generated draft and the review comments. If the workflow only returns the draft, confirm that the edge from `draft` to `review` is set correctly.## Step 6: Replace automatic routing with a specified modelIf you want more deterministic model behavior to keep the integration under tighter control, do it like this:- If Gate.AI automatic routing is enabled and your account supports it, you can use `model="auto"` for initial testing- For reproducible results, consistent evaluations, latency testing, or production review, use a specific Gate.AI model ID- Example:```pythonllm = ChatOpenAI(model="YOUR_MODEL_ID",api_key=os.environ["GATEAI_API_KEY"],base_url="https://api.gate.ai/openai/v1",temperature=0,)
Get the model ID from the Gate.AI model directory or the Gate.AI console. Don’t guess model IDs, because availability depends on your account, product status, and model-provider rules (as of June 2026).
Which configuration items matter most?
| Config item | Example value | When to use | Why it matters |
|---|---|---|---|
| API key variable | GATEAI_API_KEY | Shell and Python code | Keeps credentials out of source files |
| Base URL | /openai/v1 | ChatOpenAI(base_url=…) | Routes OpenAI-compatible requests to Gate.AI |
| Model | auto or YOUR_MODEL_ID | ChatOpenAI(model=…) | Choose automatic routing or a specific model |
| Temperature | 0 | ChatOpenAI(temperature=0) | Reduces output variation in test environments |
To keep routing behavior consistent, reuse the same llm object across LangChain and LangGraph. Only change the model parameter when switching from routing tests to fixed-model tests.
Common troubleshooting for Gate.AI LangChain and LangGraph integration
Issue: You get 401, invalid_api_key, or an authentication error
- Cause: Missing, expired, misspelled Gate.AI API key, or the current shell can’t read it
- Fix: Run
echo $GATEAI_API_KEYin the same terminal to confirm the key is valid and set in Gate.AI. If you set variables in a different session, restart the terminal.
Issue: You get 404, the connection fails, or the endpoint can’t be found
- Cause: Incorrect Base URL configuration. The correct OpenAI-compatible Base URL is
https://api.gate.ai/openai/v1 - Fix: Make sure every
ChatOpenAIinstance setsbase_urltohttps://api.gate.ai/openai/v1
Issue: Python returns ModuleNotFoundError
- Cause: Your current virtual environment didn’t install
langchain-openaiorlanggraph - Fix: After activating the virtual environment, run
pip install -U langchain langchain-openai langgraph
Issue: Authentication succeeds, but the model request fails
- Cause: The selected model isn’t available, the model name is misspelled, or it doesn’t support the current request
- Fix: Test first with
model="auto". If you need a fixed model, copy a valid model ID from Gate.AI
Issue: LangGraph workflow returns incomplete state
- Cause: A node didn’t return the expected state keys, or the graph is missing edges
- Fix: Confirm each node returns a dictionary that includes the correct keys, and ensure the graph structure includes
START, edges for each node, andEND
What else can you configure or build next?
- Use Gate.AI Developer API Integration to connect your local workflow to a broader Gate.AI API ecosystem
- If you want to integrate Gate.AI into an AI programming editor, see Gate.AI Cursor Integration Guide
- If your workflow involves Claude Code and an Anthropic-compatible configuration, see Gate.AI Claude Code Integration Guide
Frequently asked questions
Can LangChain and LangGraph share the same Gate.AI configuration?
Yes. Create one ChatOpenAI object that contains the Gate.AI API key, Base URL, and chosen model, then reuse it in your LangChain chain or LangGraph node functions.
Should I use auto or a specific model ID?
If Gate.AI automatic routing is enabled, auto is recommended for initial testing. For reproducible results, controllable evaluations, or production review, use a specific Gate.AI model ID.
Why does the Base URL need to include /openai/v1?
Gate.AI uses https://api.gate.ai/openai/v1 as the OpenAI-compatible request path. LangChain’s ChatOpenAI should point to that Base URL, not a shorter /v1 path.
Do I need to modify LangGraph itself for this integration?
No. LangGraph only calls the model object inside node functions. All Gate.AI-related configuration is done in the ChatOpenAI setup.


