The createAgent() function in LangChain is a factory that creates a ReAct agent by connecting an LLM with tools, middleware, and various configuration options for state management, structured outputs, and execution control.
The createAgent() function in LangChain (JavaScript/TypeScript SDK) is the primary entry point for building intelligent agents. It combines a language model with a set of tools and executes them in a loop until the model determines it has a final answer . The agent works by repeatedly calling the LLM with the current conversation history; if the LLM decides to call a tool, the agent executes it, adds the result to the conversation, and loops back to the LLM until a stopping condition is met .
model: The language model to drive the agent. Can be a string identifier like "anthropic:claude-sonnet-4-5-20250929" or an instance of a chat model like new ChatOpenAI() . This parameter is required.
tools: An array of tools the agent can use. Tools are defined using the tool() helper function, which requires name, description, and a schema (Zod object for TypeScript) . If omitted or empty, the agent will be a simple conversational agent without tool-calling ability .
systemPrompt: A string or SystemMessage that sets the system prompt for the agent. This provides high-level instructions that guide the agent's behavior throughout the conversation .
responseFormat: An optional configuration for structured responses. Accepts a ToolStrategy, ProviderStrategy, or a Pydantic/Zod model class. When provided, the agent will handle structured output during the conversation flow .
middleware: A sequence of AgentMiddleware instances that intercept and modify agent behavior at various lifecycle stages. Middleware can hook into: before_agent (runs once on invocation, good for loading memory), before_model (fires before each model call, ideal for trimming history or PII detection), wrap_model_call (wraps the entire model call for caching, retries), wrap_tool_call (wraps tool execution for validation or enrichment), after_model (runs after model response, natural for human-in-the-loop), and after_agent (runs on completion for cleanup) .
stateSchema: An optional Zod object or StateSchema extending AgentState. This defines typed state fields that are persisted between multiple invocations. Can include reducers and special handling through LangGraph's state management .
contextSchema: A Zod object defining the shape of runtime context passed to each invocation. Context is read-only, not persisted between calls, and ideal for passing user IDs, permissions, or tenant data .
checkpointer: A BaseCheckpointSaver instance (or true) that enables conversation persistence. Used for saving graph state for a single thread (single conversation) .
store: A BaseStore instance for persisting data across multiple threads (multiple conversations/users). Different from checkpointer, which is conversation-specific .
version: Determines graph version for tool execution. "v1" processes all tool calls concurrently via Promise.all inside a single node. "v2" dispatches each tool call as an independent graph task using Send API, offering per-tool-call checkpointing, better fault isolation, and interrupt() support .
interrupt_before: A list of node names to interrupt execution before. Useful for adding human-in-the-loop approval before tool execution .
interrupt_after: A list of node names to interrupt after. Useful for returning directly or running additional post-processing .
debug: Boolean flag to enable verbose logging for graph execution. When enabled, prints detailed information about each node execution, state updates, and transitions .
signal: An AbortSignal for cancelling the agent call. If provided, the call will be aborted when the signal is aborted .
responseFormat: This parameter is marked deprecated and will be removed in future versions. Use the response_format property in the configuration instead .
systemPrompt: Also marked deprecated—use system_prompt instead .
prompt: The framework supports custom prompts beyond the default ReAct template, allowing you to fully control the agent's reasoning format .
name: An optional name for the CompiledStateGraph. Automatically used when adding the agent graph to another graph as a subgraph node, particularly useful for multi-agent systems .
Tools are fixed at agent creation time. You cannot pass a different tools array to agent.invoke(). For per-session or per-user tool sets, build the agent dynamically; for turn-by-turn control, use middleware to update tool descriptions and guidance .
The tools array can be empty or omitted entirely, in which case the agent will function as a simple conversational agent without tool-calling capabilities .
You can use model string identifiers like "openai:gpt-4o" or "anthropic:claude-sonnet-4-5-20250929" for simplified configuration, or pass a fully initialized chat model instance .