langchain.js
    Preparing search index...

    Function createAgent

    • 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:

      • String identifier: "openai:gpt-4o" for simple setup
      • Model instance: Configured model object for full control
      • Dynamic function: Select models at runtime based on state

      Tools give agents the ability to take actions:

      • Pass an array of tools created with the tool function
      • Or provide a configured ToolNode for custom error handling

      Shape how your agent approaches tasks:

      • String for simple instructions
      • SystemMessage for structured prompts
      • Function for dynamic prompts based on state

      Middleware allows you to extend the agent's behavior:

      • Add pre/post-model processing for context injection or validation
      • Add dynamic control flows, e.g. terminate invocation or retries
      • Add human-in-the-loop capabilities
      • Add tool calls to the agent
      • Add tool results to the agent
      • Structured Output: Use responseFormat with a Zod schema to get typed responses
      • Memory: Extend the state schema to remember information across interactions
      • Streaming: Get real-time updates as the agent processes

      Type Parameters

      • T extends Record<string, any> = Record<string, any>
      • StateSchema extends any = undefined
      • ContextSchema extends any = AnyAnnotationRoot
      • const TMiddleware extends readonly AgentMiddleware<any, any, any>[] = readonly AgentMiddleware<any, any, any>[]

      Parameters

      Returns ReactAgent<T, StateSchema, ContextSchema, TMiddleware>

      A ReactAgent instance with invoke and stream methods

      import { 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) {
      // ...
      }
    • 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:

      • String identifier: "openai:gpt-4o" for simple setup
      • Model instance: Configured model object for full control
      • Dynamic function: Select models at runtime based on state

      Tools give agents the ability to take actions:

      • Pass an array of tools created with the tool function
      • Or provide a configured ToolNode for custom error handling

      Shape how your agent approaches tasks:

      • String for simple instructions
      • SystemMessage for structured prompts
      • Function for dynamic prompts based on state

      Middleware allows you to extend the agent's behavior:

      • Add pre/post-model processing for context injection or validation
      • Add dynamic control flows, e.g. terminate invocation or retries
      • Add human-in-the-loop capabilities
      • Add tool calls to the agent
      • Add tool results to the agent
      • Structured Output: Use responseFormat with a Zod schema to get typed responses
      • Memory: Extend the state schema to remember information across interactions
      • Streaming: Get real-time updates as the agent processes

      Type Parameters

      • T extends readonly InteropZodType<any>[]
      • StateSchema extends any = undefined
      • ContextSchema extends any = AnyAnnotationRoot
      • const TMiddleware extends readonly AgentMiddleware<any, any, any>[] = readonly AgentMiddleware<any, any, any>[]

      Parameters

      Returns ReactAgent<
          ExtractZodArrayTypes<T> extends Record<string, any>
              ? ExtractZodArrayTypes<ExtractZodArrayTypes<T>>
              : Record<string, any>,
          StateSchema,
          ContextSchema,
          TMiddleware,
      >

      A ReactAgent instance with invoke and stream methods

      import { 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) {
      // ...
      }
    • 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:

      • String identifier: "openai:gpt-4o" for simple setup
      • Model instance: Configured model object for full control
      • Dynamic function: Select models at runtime based on state

      Tools give agents the ability to take actions:

      • Pass an array of tools created with the tool function
      • Or provide a configured ToolNode for custom error handling

      Shape how your agent approaches tasks:

      • String for simple instructions
      • SystemMessage for structured prompts
      • Function for dynamic prompts based on state

      Middleware allows you to extend the agent's behavior:

      • Add pre/post-model processing for context injection or validation
      • Add dynamic control flows, e.g. terminate invocation or retries
      • Add human-in-the-loop capabilities
      • Add tool calls to the agent
      • Add tool results to the agent
      • Structured Output: Use responseFormat with a Zod schema to get typed responses
      • Memory: Extend the state schema to remember information across interactions
      • Streaming: Get real-time updates as the agent processes

      Type Parameters

      • StateSchema extends any = undefined
      • ContextSchema extends any = AnyAnnotationRoot
      • const TMiddleware extends readonly AgentMiddleware<any, any, any>[] = readonly AgentMiddleware<any, any, any>[]

      Parameters

      Returns ReactAgent<Record<string, unknown>, StateSchema, ContextSchema, TMiddleware>

      A ReactAgent instance with invoke and stream methods

      import { 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) {
      // ...
      }
    • 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:

      • String identifier: "openai:gpt-4o" for simple setup
      • Model instance: Configured model object for full control
      • Dynamic function: Select models at runtime based on state

      Tools give agents the ability to take actions:

      • Pass an array of tools created with the tool function
      • Or provide a configured ToolNode for custom error handling

      Shape how your agent approaches tasks:

      • String for simple instructions
      • SystemMessage for structured prompts
      • Function for dynamic prompts based on state

      Middleware allows you to extend the agent's behavior:

      • Add pre/post-model processing for context injection or validation
      • Add dynamic control flows, e.g. terminate invocation or retries
      • Add human-in-the-loop capabilities
      • Add tool calls to the agent
      • Add tool results to the agent
      • Structured Output: Use responseFormat with a Zod schema to get typed responses
      • Memory: Extend the state schema to remember information across interactions
      • Streaming: Get real-time updates as the agent processes

      Type Parameters

      • StateSchema extends any = undefined
      • ContextSchema extends any = AnyAnnotationRoot
      • const TMiddleware extends readonly AgentMiddleware<any, any, any>[] = readonly AgentMiddleware<any, any, any>[]

      Parameters

      Returns ReactAgent<Record<string, unknown>, StateSchema, ContextSchema, TMiddleware>

      A ReactAgent instance with invoke and stream methods

      import { 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) {
      // ...
      }
    • 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:

      • String identifier: "openai:gpt-4o" for simple setup
      • Model instance: Configured model object for full control
      • Dynamic function: Select models at runtime based on state

      Tools give agents the ability to take actions:

      • Pass an array of tools created with the tool function
      • Or provide a configured ToolNode for custom error handling

      Shape how your agent approaches tasks:

      • String for simple instructions
      • SystemMessage for structured prompts
      • Function for dynamic prompts based on state

      Middleware allows you to extend the agent's behavior:

      • Add pre/post-model processing for context injection or validation
      • Add dynamic control flows, e.g. terminate invocation or retries
      • Add human-in-the-loop capabilities
      • Add tool calls to the agent
      • Add tool results to the agent
      • Structured Output: Use responseFormat with a Zod schema to get typed responses
      • Memory: Extend the state schema to remember information across interactions
      • Streaming: Get real-time updates as the agent processes

      Type Parameters

      • StateSchema extends any = undefined
      • ContextSchema extends any = AnyAnnotationRoot
      • const TMiddleware extends readonly AgentMiddleware<any, any, any>[] = readonly AgentMiddleware<any, any, any>[]

      Parameters

      Returns ReactAgent<Record<string, unknown>, StateSchema, ContextSchema, TMiddleware>

      A ReactAgent instance with invoke and stream methods

      import { 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) {
      // ...
      }
    • 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:

      • String identifier: "openai:gpt-4o" for simple setup
      • Model instance: Configured model object for full control
      • Dynamic function: Select models at runtime based on state

      Tools give agents the ability to take actions:

      • Pass an array of tools created with the tool function
      • Or provide a configured ToolNode for custom error handling

      Shape how your agent approaches tasks:

      • String for simple instructions
      • SystemMessage for structured prompts
      • Function for dynamic prompts based on state

      Middleware allows you to extend the agent's behavior:

      • Add pre/post-model processing for context injection or validation
      • Add dynamic control flows, e.g. terminate invocation or retries
      • Add human-in-the-loop capabilities
      • Add tool calls to the agent
      • Add tool results to the agent
      • Structured Output: Use responseFormat with a Zod schema to get typed responses
      • Memory: Extend the state schema to remember information across interactions
      • Streaming: Get real-time updates as the agent processes

      Type Parameters

      • T extends Record<string, any> = Record<string, any>
      • StateSchema extends any = undefined
      • ContextSchema extends any = AnyAnnotationRoot
      • const TMiddleware extends readonly AgentMiddleware<any, any, any>[] = readonly AgentMiddleware<any, any, any>[]

      Parameters

      Returns ReactAgent<T, StateSchema, ContextSchema, TMiddleware>

      A ReactAgent instance with invoke and stream methods

      import { 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) {
      // ...
      }
    • 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:

      • String identifier: "openai:gpt-4o" for simple setup
      • Model instance: Configured model object for full control
      • Dynamic function: Select models at runtime based on state

      Tools give agents the ability to take actions:

      • Pass an array of tools created with the tool function
      • Or provide a configured ToolNode for custom error handling

      Shape how your agent approaches tasks:

      • String for simple instructions
      • SystemMessage for structured prompts
      • Function for dynamic prompts based on state

      Middleware allows you to extend the agent's behavior:

      • Add pre/post-model processing for context injection or validation
      • Add dynamic control flows, e.g. terminate invocation or retries
      • Add human-in-the-loop capabilities
      • Add tool calls to the agent
      • Add tool results to the agent
      • Structured Output: Use responseFormat with a Zod schema to get typed responses
      • Memory: Extend the state schema to remember information across interactions
      • Streaming: Get real-time updates as the agent processes

      Type Parameters

      • T extends Record<string, any> = Record<string, any>
      • StateSchema extends any = undefined
      • ContextSchema extends any = AnyAnnotationRoot
      • const TMiddleware extends readonly AgentMiddleware<any, any, any>[] = readonly AgentMiddleware<any, any, any>[]

      Parameters

      Returns ReactAgent<T, StateSchema, ContextSchema, TMiddleware>

      A ReactAgent instance with invoke and stream methods

      import { 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) {
      // ...
      }
    • 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:

      • String identifier: "openai:gpt-4o" for simple setup
      • Model instance: Configured model object for full control
      • Dynamic function: Select models at runtime based on state

      Tools give agents the ability to take actions:

      • Pass an array of tools created with the tool function
      • Or provide a configured ToolNode for custom error handling

      Shape how your agent approaches tasks:

      • String for simple instructions
      • SystemMessage for structured prompts
      • Function for dynamic prompts based on state

      Middleware allows you to extend the agent's behavior:

      • Add pre/post-model processing for context injection or validation
      • Add dynamic control flows, e.g. terminate invocation or retries
      • Add human-in-the-loop capabilities
      • Add tool calls to the agent
      • Add tool results to the agent
      • Structured Output: Use responseFormat with a Zod schema to get typed responses
      • Memory: Extend the state schema to remember information across interactions
      • Streaming: Get real-time updates as the agent processes

      Type Parameters

      • T extends Record<string, any> = Record<string, any>
      • StateSchema extends any = undefined
      • ContextSchema extends any = AnyAnnotationRoot
      • const TMiddleware extends readonly AgentMiddleware<any, any, any>[] = readonly AgentMiddleware<any, any, any>[]

      Parameters

      Returns ReactAgent<T, StateSchema, ContextSchema, TMiddleware>

      A ReactAgent instance with invoke and stream methods

      import { 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) {
      // ...
      }
    • 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:

      • String identifier: "openai:gpt-4o" for simple setup
      • Model instance: Configured model object for full control
      • Dynamic function: Select models at runtime based on state

      Tools give agents the ability to take actions:

      • Pass an array of tools created with the tool function
      • Or provide a configured ToolNode for custom error handling

      Shape how your agent approaches tasks:

      • String for simple instructions
      • SystemMessage for structured prompts
      • Function for dynamic prompts based on state

      Middleware allows you to extend the agent's behavior:

      • Add pre/post-model processing for context injection or validation
      • Add dynamic control flows, e.g. terminate invocation or retries
      • Add human-in-the-loop capabilities
      • Add tool calls to the agent
      • Add tool results to the agent
      • Structured Output: Use responseFormat with a Zod schema to get typed responses
      • Memory: Extend the state schema to remember information across interactions
      • Streaming: Get real-time updates as the agent processes

      Type Parameters

      • StateSchema extends any = undefined
      • ContextSchema extends any = AnyAnnotationRoot
      • const TMiddleware extends readonly AgentMiddleware<any, any, any>[] = readonly AgentMiddleware<any, any, any>[]

      Parameters

      Returns ReactAgent<ResponseFormatUndefined, StateSchema, ContextSchema, TMiddleware>

      A ReactAgent instance with invoke and stream methods

      import { 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) {
      // ...
      }
    • 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:

      • String identifier: "openai:gpt-4o" for simple setup
      • Model instance: Configured model object for full control
      • Dynamic function: Select models at runtime based on state

      Tools give agents the ability to take actions:

      • Pass an array of tools created with the tool function
      • Or provide a configured ToolNode for custom error handling

      Shape how your agent approaches tasks:

      • String for simple instructions
      • SystemMessage for structured prompts
      • Function for dynamic prompts based on state

      Middleware allows you to extend the agent's behavior:

      • Add pre/post-model processing for context injection or validation
      • Add dynamic control flows, e.g. terminate invocation or retries
      • Add human-in-the-loop capabilities
      • Add tool calls to the agent
      • Add tool results to the agent
      • Structured Output: Use responseFormat with a Zod schema to get typed responses
      • Memory: Extend the state schema to remember information across interactions
      • Streaming: Get real-time updates as the agent processes

      Type Parameters

      • StateSchema extends any = undefined
      • ContextSchema extends any = AnyAnnotationRoot
      • const TMiddleware extends readonly AgentMiddleware<any, any, any>[] = readonly AgentMiddleware<any, any, any>[]

      Parameters

      Returns ReactAgent<ResponseFormatUndefined, StateSchema, ContextSchema, TMiddleware>

      A ReactAgent instance with invoke and stream methods

      import { 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) {
      // ...
      }
    • 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:

      • String identifier: "openai:gpt-4o" for simple setup
      • Model instance: Configured model object for full control
      • Dynamic function: Select models at runtime based on state

      Tools give agents the ability to take actions:

      • Pass an array of tools created with the tool function
      • Or provide a configured ToolNode for custom error handling

      Shape how your agent approaches tasks:

      • String for simple instructions
      • SystemMessage for structured prompts
      • Function for dynamic prompts based on state

      Middleware allows you to extend the agent's behavior:

      • Add pre/post-model processing for context injection or validation
      • Add dynamic control flows, e.g. terminate invocation or retries
      • Add human-in-the-loop capabilities
      • Add tool calls to the agent
      • Add tool results to the agent
      • Structured Output: Use responseFormat with a Zod schema to get typed responses
      • Memory: Extend the state schema to remember information across interactions
      • Streaming: Get real-time updates as the agent processes

      Type Parameters

      • StructuredResponseFormat extends Record<string, any> = Record<string, any>
      • StateSchema extends any = undefined
      • ContextSchema extends any = AnyAnnotationRoot
      • const TMiddleware extends readonly AgentMiddleware<any, any, any>[] = readonly AgentMiddleware<any, any, any>[]

      Parameters

      Returns ReactAgent<StructuredResponseFormat, StateSchema, ContextSchema, TMiddleware>

      A ReactAgent instance with invoke and stream methods

      import { 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) {
      // ...
      }