Futures
Access hundreds of perpetual contracts
CFD
Gold
One platform for global traditional assets
Options
Hot
Trade European-style vanilla options
Unified Account
Maximize your capital efficiency
Demo Trading
Introduction to Futures Trading
Learn the basics of futures trading
Futures Events
Join events to earn rewards
Demo Trading
Use virtual funds to practice risk-free trading
CFD
U.S. stock CFD derivatives
US Stocks
Access real US stocks and ETFs
HK Stocks
Trade quality Hong Kong-listed stocks
Korean Stocks
SK Hynix
Real Korean stocks and top assets
Stock Futures
High leverage, 24/7 trading
Tokenized Stocks
Backed by real stock assets
IPO Access
Unlock full access to global stock IPOs
GUSD
Mint GUSD for Treasury RWA yields
Stocks Activities
Trade Popular Stocks and Unlock Generous Airdrops
Launch
CandyDrop
Collect candies to earn airdrops
Launchpool
Quick staking, earn potential new tokens
HODLer Airdrop
Hold GT and get massive airdrops for free
IPO Access
Unlock full access to global stock IPOs
Alpha Points
Trade on-chain assets and earn airdrops
Futures Points
Earn futures points and claim airdrop rewards
Promotions
AI
Gate AI
Your all-in-one conversational AI partner
Gate AI Bot
Use Gate AI directly in your social App
GateClaw
Gate Blue Lobster, ready to go
Gate for AI Agent
AI infrastructure, Gate MCP, Skills, and CLI
Gate Skills Hub
10K+ Skills
From office tasks to trading, the all-in-one skill hub makes AI even more useful.
How to integrate Gate.AI into LangChain and LangGraph
Gate.AI LangChain and LangGraph Integration Guide
Gate.AI provides an API endpoint compatible with OpenAI, allowing developers to connect LangChain and LangGraph through this endpoint to route model calls via Gate.AI. When a Python application needs chain-based prompts, graph-based agent workflows, or aims to build a unified model gateway without rewriting application logic for each provider, this solution is especially important. This article covers local environment setup, LangChain test calls, prompt chains, and a simple LangGraph workflow. It does not include production deployment, vector databases, observability, billing configuration, or enterprise access policies.
Prerequisites
Source: Gate.AI official documentation and product materials, as of June 2026.
What capabilities will you gain after completing this guide?
You will be able to connect Gate.AI to LangChain via ChatOpenAI and reuse the same model configuration within LangGraph workflows.
This solution helps you to:
For broader API integration background, see Gate.AI Developer API Integration.
Step 1: Install Python dependencies
This step installs the necessary LangChain OpenAI integration and LangGraph packages for local workflows.
Create and activate a virtual environment:
bash python -m venv .venv source .venv/bin/activate
pip install -U langchain langchain-openai langgraph
Activation command for Windows PowerShell:
powershell .venv\Scripts\Activate.ps1
After installation, you should be able to import langchain_openai and langgraph without issues.
Step 2: Store your Gate.AI API key
This step saves your Gate.AI API key outside of source code.
In bash, set environment variable:
bash export GATEAI_API_KEY="YOUR_API_KEY"
In Windows PowerShell:
powershell setx GATEAI_API_KEY "YOUR_API_KEY"
Note: After using setx, restart your PowerShell session.
Do not commit your real API key to Git. For team projects, use a secret manager, CI key configuration, or approved internal environment variable workflows.
Step 3: Configure Gate.AI in LangChain
This step creates a chat model in LangChain that sends requests compatible with OpenAI protocol to Gate.AI.
According to Gate.AI documentation as of June 2026, the OpenAI-compatible base URL is:
In LangChain, set this URL as the base_url. No need to append /chat/completions; LangChain handles the path automatically.
Example:
python import os from langchain_openai import ChatOpenAI
llm = ChatOpenAI( model="auto", api_key=os.environ["GATEAI_API_KEY"], base_url="", 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 appropriate models based on tasks, routing rules, or configurations.
Actual response may vary, as Gate.AI routing dynamically responds based on the selected model.
Step 4: Build a LangChain prompt chain
This step connects reusable prompts, Gate.AI-supported models, and string output parsers.
Example:
python import os from langchain_openai import ChatOpenAI from langchain_core.prompts import ChatPromptTemplate from langchain_core.output_parsers import StrOutputParser
llm = ChatOpenAI( model="auto", api_key=os.environ["GATEAI_API_KEY"], base_url="", 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 will see a concise explanation with three key points. If the script errors before returning text, check your API key, base_url, and model configuration first, rather than modifying the chain structure directly.
Step 5: Configure Gate.AI in LangGraph
This step reuses the same Gate.AI model configuration within a LangGraph state workflow.
The example creates a node that generates a brief explanation, another node that reviews it, keeping the flow simple for initial validation 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, END
llm = ChatOpenAI( model="auto", api_key=os.environ["GATEAI_API_KEY"], base_url="", temperature=0, )
class WorkflowState(TypedDict): topic: str draft: str review: 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:\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 will see the generated draft and review comments. If only the draft is returned, verify that the edge from draft to review is correctly set.
Step 6: Replace auto routing with a specific model
To make integration more controllable by fixing the model behavior:
If Gate.AI auto-routing is enabled and your account supports it, you can test initially with model="auto"
For reproducible results, evaluation consistency, latency testing, or production review, use a specific Gate.AI model ID
Example:
python llm = ChatOpenAI( model="YOUR_MODEL_ID", api_key=os.environ["GATEAI_API_KEY"], base_url="", temperature=0, )
Model IDs can be obtained from the Gate.AI model catalog or console. Do not guess model IDs arbitrarily, as availability depends on your account, product status, and provider rules (as of June 2026).
Which configuration options are most critical?
| Config Item | Example Value | Use Case | Importance Explanation | | ----------------- | ---------------------------------------------------------- | ---------------------------- | ----------------------------------------------------- | | API Key Variable | GATEAI_API_KEY | Shell and Python code | Keeps credentials out of source code | | Base URL | | ChatOpenAI(base_url=...) | Routes OpenAI-compatible requests to Gate.AI | | Model | auto or YOUR_MODEL_ID | ChatOpenAI(model=...) | Choose auto-routing or specify a model | | Temperature | 0 | ChatOpenAI(temperature=0) | Reduces output variability in testing environments |
To keep routing behavior consistent, it’s recommended to share the same llm object between LangChain and LangGraph. Only modify the model parameter when switching from routing test to fixed model testing.
Common troubleshooting for Gate.AI LangChain and LangGraph integration
Issue: Request returns 401, invalid_api_key, or authentication error
Issue: Request returns 404, connection failure, or endpoint not found
Cause: Incorrect base URL configuration. Correct OpenAI-compatible base URL is:
Solution: Ensure each ChatOpenAI instance’s base_url is set to the correct URL.
Issue: Python raises ModuleNotFoundError
Issue: Authentication succeeds but model request fails
Issue: LangGraph workflow returns incomplete state
Next steps: what can you configure or build?
FAQs
Can LangChain and LangGraph share the same Gate.AI configuration?
Yes. Just create a ChatOpenAI object with the Gate.AI API key, base URL, and chosen model, then reuse it in LangChain chains or LangGraph node functions.
Should I choose auto or specify a model ID?
If Gate.AI auto-routing is enabled, initial testing with auto is recommended. For reproducibility, evaluation, or production, use a specific Gate.AI model ID.
Why should the base URL include /openai/v1?
Gate.AI uses /openai/v1 as the request path compatible with OpenAI. LangChain’s ChatOpenAI should point to this base URL, not just /v1.
Do I need to modify LangGraph itself for this integration?
No. LangGraph only calls the model object within node functions; all Gate.AI configuration is handled within the ChatOpenAI setup.