Skip to main content
Agents combine language models with tools to create systems that can reason about tasks, decide which tools to use, and iteratively work towards solutions. createAgent() provides a production-ready agent implementation. An LLM Agent runs tools in a loop to achieve a goal. An agent runs until a stop condition is met - i.e., when the model emits a final output or an iteration limit is reached.
createAgent() builds a graph-based agent runtime using LangGraph. A graph consists of nodes (steps) and edges (connections) that define how your agent processes information. The agent moves through this graph, executing nodes like the model node (which calls the model), the tools node (which executes tools), or middleware.Learn more about the Graph API.

Core components

Model

The model is the reasoning engine of your agent. It can be specified in multiple ways, supporting both static and dynamic model selection.

Static model

Static models are configured once when creating the agent and remain unchanged throughout execution. This is the most common and straightforward approach. To initialize a static model from a :
Model identifier strings use the format provider:model (e.g. "openai:gpt-5"). You may want more control over the model configuration, in which case you can initialize a model instance directly using the provider package:
Model instances give you complete control over configuration. Use them when you need to set specific parameters like temperature, max_tokens, timeouts, or configure API keys, base_url, and other provider-specific settings. Refer to the API reference to see available params and methods on your model.

Dynamic model

Dynamic models are selected at based on the current and context. This enables sophisticated routing logic and cost optimization. To use a dynamic model, create middleware with wrapModelCall that modifies the model in the request:
For more details on middleware and advanced patterns, see the middleware documentation.
For model configuration details, see Models. For dynamic model selection patterns, see Dynamic model in middleware.

Tools

Tools give agents the ability to take actions. Agents go beyond simple model-only tool binding by facilitating:
  • Multiple tool calls in sequence (triggered by a single prompt)
  • Parallel tool calls when appropriate
  • Dynamic tool selection based on previous results
  • Tool retry logic and error handling
  • State persistence across tool calls
For more information, see Tools.

Static tools

Static tools are defined when creating the agent and remain unchanged throughout execution. This is the most common and straightforward approach. To define an agent with static tools, pass a list of the tools to the agent.
If an empty tool list is provided, the agent will consist of a single LLM node without tool-calling capabilities.

Dynamic tools

With dynamic tools, the set of tools available to the agent is modified at runtime rather than defined all upfront. Not every tool is appropriate for every situation. Too many tools may overwhelm the model (overload context) and increase errors; too few limit capabilities. Dynamic tool selection enables adapting the available toolset based on authentication state, user permissions, feature flags, or conversation stage. There are two approaches depending on whether tools are known ahead of time:
When all possible tools are known at agent creation time, you can pre-register them and dynamically filter which ones are exposed to the model based on state, permissions, or context.
Enable advanced tools only after certain conversation milestones:
This approach is best when:
  • All possible tools are known at compile/startup time
  • You want to filter based on permissions, feature flags, or conversation state
  • Tools are static but their availability is dynamic
See Dynamically selecting tools for more examples.
To learn more about tools, see Tools.

Tool error handling

To customize how tool errors are handled, use the wrapToolCall hook in a custom middleware:
The agent will return a ToolMessage with the custom error message when a tool fails.

Tool use in the ReAct loop

Agents follow the ReAct (“Reasoning + Acting”) pattern, alternating between brief reasoning steps with targeted tool calls and feeding the resulting observations into subsequent decisions until they can deliver a final answer.
Prompt: Identify the current most popular wireless headphones and verify availability.
  • Reasoning: “Popularity is time-sensitive, I need to use the provided search tool.”
  • Acting: Call search_products("wireless headphones")
  • Reasoning: “I need to confirm availability for the top-ranked item before answering.”
  • Acting: Call check_inventory("WH-1000XM5")
  • Reasoning: “I have the most popular model and its stock status. I can now answer the user’s question.”
  • Acting: Produce final answer

System prompt

You can shape how your agent approaches tasks by providing a prompt. The systemPrompt parameter can be provided as a string:
When no systemPrompt is provided, the agent will infer its task from the messages directly. The systemPrompt parameter accepts either a string or a SystemMessage. Using a SystemMessage gives you more control over the prompt structure, which is useful for provider-specific features like Anthropic’s prompt caching:
The cache_control field with { type: "ephemeral" } tells Anthropic to cache that content block, reducing latency and costs for repeated requests that use the same system prompt.

Dynamic system prompt

For more advanced use cases where you need to modify the system prompt based on runtime context or agent state, you can use middleware.
For more details on message types and formatting, see Messages. For comprehensive middleware documentation, see Middleware.

Name

Set an optional name for the agent. This is used as the node identifier when adding the agent as a subgraph in multi-agent systems:
Prefer snake_case for agent names (e.g., research_assistant instead of Research Assistant). Some model providers reject names containing spaces or special characters with errors. Using alphanumeric characters, underscores, and hyphens only ensures compatibility across all providers. The same applies to tool names.

Invocation

You can invoke an agent by passing an update to its State. All agents include a sequence of messages in their state; to invoke the agent, pass a new message:
For streaming steps and / or tokens from the agent, refer to the streaming guide. Otherwise, the agent follows the LangGraph Graph API and supports all associated methods, such as stream and invoke.
Use LangSmith to trace, debug, and evaluate your agents.

Advanced concepts

Structured output

In some situations, you may want the agent to return an output in a specific format. LangChain provides a simple, universal way to do this with the responseFormat parameter.
To learn about structured output, see Structured output.

Memory

Agents maintain conversation history automatically through the message state. You can also configure the agent to use a custom state schema to remember additional information during the conversation. Information stored in the state can be thought of as the short-term memory of the agent:
To learn more about memory, see Memory. For information on implementing long-term memory that persists across sessions, see Long-term memory.

Streaming

We’ve seen how the agent can be called with invoke to get a final response. If the agent executes multiple steps, this may take a while. To show intermediate progress, we can stream back messages as they occur.
For more details on streaming, see Streaming.

Middleware

Middleware provides powerful extensibility for customizing agent behavior at different stages of execution. You can use middleware to:
  • Process state before the model is called (e.g., message trimming, context injection)
  • Modify or validate the model’s response (e.g., guardrails, content filtering)
  • Handle tool execution errors with custom logic
  • Implement dynamic model selection based on state or context
  • Add custom logging, monitoring, or analytics
Middleware integrates seamlessly into the agent’s execution, allowing you to intercept and modify data flow at key points without changing the core agent logic.
For comprehensive middleware documentation including hooks like beforeModel, afterModel, and wrapToolCall, see Middleware.