Gate.AIBlogHow to Integrate Gate.AI with LangChain and LangGraph

    How to Integrate Gate.AI with LangChain and LangGraph

    Guides

    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 auto with 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:

      1. python -m venv .venv
      2. source .venv/bin/activate
      3. pip install -U langchain langchain-openai langgraph
    • Activate command in Windows PowerShell:

      1. .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:

      1. export GATEAI_API_KEY="YOUR_API_KEY"
    • Set in Windows PowerShell:

      1. 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:

      1. https://api.gate.ai/openai/v1
    • In LangChain, set this address as base_url. You do not need to append /chat/completions after base_url; LangChain will handle the path automatically.

    • Example:

      1. import os
      2. from langchain_openai import ChatOpenAI
      3. llm = ChatOpenAI(
      4. model="auto",
      5. api_key=os.environ["GATEAI_API_KEY"],
      6. base_url="https://api.gate.ai/openai/v1",
      7. temperature=0,
      8. )
      9. response = llm.invoke("Write one sentence explaining what an AI model router does.")
      10. 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:

      1. import os
      2. from langchain_openai import ChatOpenAI
      3. from langchain_core.prompts import ChatPromptTemplate
      4. from langchain_core.output_parsers import StrOutputParser
      5. llm = ChatOpenAI(
      6. model="auto",
      7. api_key=os.environ["GATEAI_API_KEY"],
      8. base_url="XX/openai/v1",
      9. temperature=0,
      10. )
      11. prompt = ChatPromptTemplate.from_messages(
      12. [
      13. ("system", "You are a concise technical assistant."),
      14. ("human", "Explain {topic} in three bullet points."),
      15. ]
      16. )
      17. chain = prompt | llm | StrOutputParser()
      18. result = chain.invoke({"topic": "Gate.AI API routing"})
      19. 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, END

      llm = ChatOpenAI(

      1. model="auto",
      2. api_key=os.environ["GATEAI_API_KEY"],
      3. base_url="https://api.gate.ai/openai/v1",
      4. temperature=0,

      )

      class WorkflowState(TypedDict):

      1. topic: str
      2. draft: str
      3. review: str

      def draft_node(state: WorkflowState) -> dict:

      1. response = llm.invoke(
      2. [
      3. ("system", "You write short technical explanations."),
      4. ("human", f"Write a two-sentence explanation of {state['topic']}."),
      5. ]
      6. )
      7. return {"draft": response.content}

      def review_node(state: WorkflowState) -> dict:

      1. response = llm.invoke(
      2. [
      3. ("system", "You review technical writing for clarity."),
      4. ("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"])

    1. Youll 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.
    2. ## Step 6: Replace automatic routing with a specified model
    3. If you want more deterministic model behavior to keep the integration under tighter control, do it like this:
    4. - If Gate.AI automatic routing is enabled and your account supports it, you can use `model="auto"` for initial testing
    5. - For reproducible results, consistent evaluations, latency testing, or production review, use a specific Gate.AI model ID
    6. - Example:
    7. ```python
    8. llm = ChatOpenAI(
    9. model="YOUR_MODEL_ID",
    10. api_key=os.environ["GATEAI_API_KEY"],
    11. base_url="https://api.gate.ai/openai/v1",
    12. temperature=0,
    13. )

    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_KEY in 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 ChatOpenAI instance sets base_url to https://api.gate.ai/openai/v1

    Issue: Python returns ModuleNotFoundError

    • Cause: Your current virtual environment didn’t install langchain-openai or langgraph
    • 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, and END

    What else can you configure or build next?

    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.

    The content herein does not constitute any offer, solicitation, or recommendation. You should always seek independent professional advice before making any investment decisions. Please note that Gate may restrict or prohibit the use of all or a portion of the Services from Restricted Locations. For more information, please read the User Agreement

    Related Articles