I’m using LangGraph to serve an agent with langgraph dev --no-browser.
The code below provides a get_agent function that returns a CompiledStateGraph object. Notice the _agent variable is used as a global variable to prevent the function from creating a new agent (or CompiledStateGraph object) each time the function is called.
# joker_agent.py
from langchain.agents import create_agent
from langgraph.graph.state import CompiledStateGraph
from langchain_openai import AzureChatOpenAI
from langchain_mcp_adapters.client import MultiServerMCPClient
_agent: CompiledStateGraph | None = None
async def get_agent() -> CompiledStateGraph:
"""Build and cache the compiled agent graph."""
global _agent
if _agent is not None:
return _agent
mcp_client = MultiServerMCPClient(
{
"ihub": {
"transport": "http",
"url": "",
},
}
)
tools = await mcp_client.get_tools()
# Setup LLM for the agent
model = AzureChatOpenAI(
azure_endpoint="",
deployment_name="gpt-5.2",
openai_api_version="2024-12-01-preview",
model_name="gpt-5.2",
)
_agent = create_agent(model, tools, system_prompt="You are a hilarious assistant agent")
return _agent
The langgraph.json configuration file is shown below. The path points to the get_agent function which returns the agent (graph) object.
{
"$schema": "https://langgra.ph/schema.json",
"dependencies": ["."],
"graphs": {
"Joker Remote Agent": {
"path": "./src/joker_agent.py:get_agent",
"description": "Joker-style conversational agent"
}
},
"env": "./.env",
"python_version": "3.13"
}
Is it necessary to use a global object such as the _agent variable in my example for serving the agent? I typically avoid creating expensive objects each time a function is called but I’m not sure if that is necessary here. I was thinking that since the agent is being “served” to clients (users) then you don’t want the agent (graph) object being created on every request. Any guidance on how to provide the agent to the LangGraph server would be very helpful.