| Name | Type | Description |
|---|---|---|
params* | CreateAgentParams<StructuredResponseFormat, TStateSchema, ContextSchema, InteropZodType<StructuredResponseFormat>> & __type |
Creates a production-ready ReAct (Reasoning + Acting) agent that combines language models with tools and middleware to create systems that can reason about tasks, decide which tools to use, and iteratively work towards solutions.
The agent follows the ReAct pattern, interleaving reasoning steps with tool calls to iteratively work towards solutions. It can handle multiple tool calls in sequence or parallel, maintain state across interactions, and provide auditable decision processes.
The reasoning engine can be specified as:
"openai:gpt-4o" for simple setupTools give agents the ability to take actions:
tool functionToolNode for custom error handlingShape how your agent approaches tasks:
Middleware allows you to extend the agent's behavior:
responseFormat with a Zod schema to get typed responsesimport { createAgent, tool } from "langchain";
import { z } from "zod";
const search = tool(
({ query }) => `Results for: ${query}`,
{
name: "search",
description: "Search for information",
schema: z.object({
query: z.string().describe("The search query"),
})
}
);
const agent = createAgent({
llm: "openai:gpt-4o",
tools: [search],
});
const result = await agent.invoke({
messages: [{ role: "user", content: "Search for ReAct agents" }],
});import { createAgent } from "langchain";
import { z } from "zod";
const ContactInfo = z.object({
name: z.string(),
email: z.string(),
phone: z.string(),
});
const agent = createAgent({
llm: "openai:gpt-4o",
tools: [],
responseFormat: ContactInfo,
});
const result = await agent.invoke({
messages: [{
role: "user",
content: "Extract: John Doe, john@example.com, (555) 123-4567"
}],
});
console.log(result.structuredResponse);
// { name: 'John Doe', email: 'john@example.com', phone: '(555) 123-4567' }const stream = await agent.stream(
{ messages: [{ role: "user", content: "What's the weather?" }] },
{ streamMode: "values" }
);
for await (const chunk of stream) {
// ...
}import { createAgent } from "langchain";
import { StateSchema, ReducedValue } from "@langchain/langgraph";
import { z } from "zod";
const AgentState = new StateSchema({
userId: z.string(),
count: z.number().default(0),
history: new ReducedValue(
z.array(z.string()).default(() => []),
{ inputSchema: z.string(), reducer: (c, n) => [...c, n] }
),
});
const agent = createAgent({
model: "openai:gpt-4o",
tools: [searchTool],
stateSchema: AgentState,
});