How to Use LangGraph for Agents: Simple Guide
To use
langgraph for agents, you create an agent by defining its tasks and tools using Agent and Tool classes, then run it with agent.run(). LangGraph helps you build AI agents that can perform complex workflows by connecting language models and tools in a graph structure.Syntax
The basic syntax to use LangGraph for agents involves creating Tool objects that define specific actions, then combining them into an Agent. You run the agent with the run() method to get results.
- Tool: Defines a callable action with a name and function.
- Agent: Combines tools and manages the workflow.
- run(): Executes the agent with input and returns output.
python
from langgraph import Tool, Agent # Define a tool with a name and a function def greet(name: str) -> str: return f"Hello, {name}!" greet_tool = Tool(name="Greet", func=greet) # Create an agent with the tool agent = Agent(tools=[greet_tool]) # Run the agent result = agent.run("Alice") print(result)
Output
Hello, Alice!
Example
This example shows how to create a LangGraph agent that uses two tools: one to greet a user and another to say goodbye. The agent runs both tools in sequence and returns their outputs.
python
from langgraph import Tool, Agent # Define greeting tool def greet(name: str) -> str: return f"Hello, {name}!" # Define farewell tool def farewell(name: str) -> str: return f"Goodbye, {name}!" # Create tools greet_tool = Tool(name="Greet", func=greet) farewell_tool = Tool(name="Farewell", func=farewell) # Create agent with both tools agent = Agent(tools=[greet_tool, farewell_tool]) # Run agent with input result = agent.run("Bob") print(result)
Output
['Hello, Bob!', 'Goodbye, Bob!']
Common Pitfalls
Common mistakes when using LangGraph agents include:
- Not defining tools with the correct function signature (must accept input and return output).
- Forgetting to include all tools in the agent's tool list.
- Passing input that tools cannot handle, causing errors.
- Expecting
run()to return a single string when it returns a list of outputs if multiple tools are used.
python
from langgraph import Tool, Agent # Wrong: Tool function missing return def bad_tool(name: str): print(f"Hi {name}") # No return # Right: Tool function returns string def good_tool(name: str) -> str: return f"Hi {name}" bad = Tool(name="Bad", func=bad_tool) good = Tool(name="Good", func=good_tool) agent = Agent(tools=[bad, good]) # Running this will cause issues because bad_tool returns None result = agent.run("Eve") print(result) # Output will include None which may cause problems
Output
[None, 'Hi Eve']
Quick Reference
| Concept | Description |
|---|---|
| Tool | Defines an action with a name and a function that takes input and returns output. |
| Agent | Combines multiple tools to create a workflow or agent behavior. |
| run(input) | Executes the agent with the given input and returns outputs from tools. |
| Function Signature | Tool functions must accept input and return output (usually strings). |
| Output | If multiple tools, run() returns a list of outputs in order of tools. |
Key Takeaways
Create tools by defining functions that take input and return output, then wrap them with Tool.
Combine tools into an Agent and run it with agent.run(input) to get results.
Ensure tool functions always return output; missing returns cause errors.
agent.run() returns a list of outputs if multiple tools are used.
Pass input compatible with all tools to avoid runtime errors.