BUILD · 5 MIN READ
Build an agent with LangChain
Use the agent abstraction to connect a model and a small tool inventory.
Where it fits
LangChain’s agent API supplies a higher-level starting point for a model-and-tools loop. Model integrations and tool interfaces help connect the application to different services.
Prepare the example
Install langchain and langchain-openai. Set OPENAI_API_KEY and OPENAI_MODEL. Save the example as agent.py, then run python agent.py.
pip install langchain langchain-openaiimport os
from langchain.agents import create_agent
def lookup_order(order_id: str) -> str:
"""Read an order status from a local demonstration fixture."""
orders = {"A104": "Packed; awaiting courier pickup"}
return orders.get(order_id, "Order not found")
agent = create_agent(
model="openai:" + os.environ["OPENAI_MODEL"],
tools=[lookup_order],
system_prompt=(
"Use lookup_order for status questions. "
"Ground your answer in its returned data."
),
)
result = agent.invoke(
{"messages": [
{"role": "user", "content": "What is the status of A104?"}
]},
config={"recursion_limit": 8},
)
print(result["messages"][-1].content)Educational example. Uses your configured environment and credentials; no paid model call is made by this website.
Read the result
The tool uses a local demonstration fixture. The returned messages contain the tool interaction and the final answer. Test the known order and an unknown identifier before replacing the fixture with a real service.
When to reach for LangGraph
Choose explicit graph construction when your application needs custom transitions, unusual state, or a workflow whose control structure you want to own directly. Keep that choice separate from selecting a model provider.