Agent Fundamentals
An agent system consists of three core components:- Language Model: Makes decisions about which actions to take
- Tools: Functions the agent can execute
- Agent Executor: Orchestrates the agent loop
Modern LangChain agents are built using LangGraph, which provides more control and flexibility. The legacy
AgentExecutor is maintained for backwards compatibility but new development should use LangGraph.Agent Loop
The basic agent execution loop:- Observation: Receive input or tool result
- Reasoning: LLM analyzes the situation and decides next action
- Action: Execute a tool with specific inputs
- Repeat: Continue until task is complete
- Finish: Return final answer
Agent Action Schema
Agents work with structured action and observation schemas defined in/libs/core/langchain_core/agents.py:
AgentAction
Represents a tool invocation request:tool(str): Name of the tool to executetool_input(str | dict): Input to pass to the toollog(str): LLM’s reasoning before choosing this action
/libs/core/langchain_core/agents.py:44
AgentActionMessageLog
ExtendsAgentAction with full message history:
/libs/core/langchain_core/agents.py:105
AgentStep
Combines an action with its observation:/libs/core/langchain_core/agents.py:131
AgentFinish
Indicates the agent has completed its task:return_values(dict): Final output valueslog(str): Complete LLM response including reasoning
/libs/core/langchain_core/agents.py:146
Agent Patterns
ReAct Pattern
Reasoning and Acting in an interleaved manner:- Reason about the current state
- Act by calling appropriate tools
- Observe tool results
- Repeat until done
Conversational Agents
Agents that maintain conversation memory:Structured Output Agents
Agents that return structured data:Multi-Agent Systems
Multiple specialized agents collaborating:Tool Integration
Agents execute tools to interact with external systems. See the Tools documentation for details on creating tools.Tool Calling vs Function Calling
Modern chat models support native tool calling:Tool Error Handling
Handle tool failures gracefully:Agent Configuration
Max Iterations
Prevent infinite loops:Early Stopping
Control when agents should stop:Agent Prompting
Customize agent system prompts:Observability and Debugging
Trace Agent Steps
Monitor agent decision-making:Custom Callbacks
Implement custom agent monitoring:Intermediate Steps
Access all agent steps:Best Practices
Tool descriptions matter
Tool descriptions matter
Clear, detailed tool descriptions help the LLM choose the right tool:
Limit tool count
Limit tool count
Too many tools confuse the agent. Group related functions or use hierarchical agents:
Set iteration limits
Set iteration limits
Always configure max iterations to prevent runaway costs:
Validate tool inputs
Validate tool inputs
Use Pydantic models for type safety:
Migration from Legacy Agents
Legacy agent code:Next Steps
Tools
Learn to build custom tools for agents
LangGraph
Build advanced agent systems with LangGraph
Messages
Understand message types in agent interactions
Runnables
Compose agents with other components