langchain.js
    Preparing search index...

    Function createAgent

    LangChain Agents

    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

      • StateSchema extends any = AnnotationRoot<{}>
      • T extends Record<string, any> = Record<string, any>
      • ContextSchema extends any = AnyAnnotationRoot

      Parameters

      Returns ReactAgent<StateSchema, T, ContextSchema>

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution
    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

      • T extends readonly InteropZodType<any>[]
      • StateSchema extends any = AnnotationRoot<{}>
      • ContextSchema extends any = AnyAnnotationRoot

      Parameters

      • params: CreateAgentParams<
            StateSchema,
            ExtractZodArrayTypes<T> extends Record<string, any>
                ? ExtractZodArrayTypes<ExtractZodArrayTypes<T>>
                : Record<string, any>,
            ContextSchema,
            T,
        > & { responseFormat: T }

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

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution
    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

      • StateSchema extends any = AnnotationRoot<{}>
      • ContextSchema extends any = AnyAnnotationRoot

      Parameters

      • params: CreateAgentParams<
            StateSchema,
            Record<string, unknown>,
            ContextSchema,
            JsonSchemaFormat,
        > & { responseFormat: JsonSchemaFormat }

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

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution
    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

      • StateSchema extends any = AnnotationRoot<{}>
      • ContextSchema extends any = AnyAnnotationRoot

      Parameters

      • params: CreateAgentParams<
            StateSchema,
            Record<string, unknown>,
            ContextSchema,
            JsonSchemaFormat[],
        > & { responseFormat: JsonSchemaFormat[] }

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

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution
    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

      • StateSchema extends any = AnnotationRoot<{}>
      • ContextSchema extends any = AnyAnnotationRoot

      Parameters

      • params: CreateAgentParams<
            StateSchema,
            Record<string, unknown>,
            ContextSchema,
            JsonSchemaFormat | JsonSchemaFormat[],
        > & { responseFormat: JsonSchemaFormat | JsonSchemaFormat[] }

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

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution
    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

      • StateSchema extends any = AnnotationRoot<{}>
      • T extends Record<string, any> = Record<string, any>
      • ContextSchema extends any = AnyAnnotationRoot

      Parameters

      Returns ReactAgent<StateSchema, T, ContextSchema>

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution
    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

      • StateSchema extends any = AnnotationRoot<{}>
      • T extends Record<string, any> = Record<string, any>
      • ContextSchema extends any = AnyAnnotationRoot

      Parameters

      Returns ReactAgent<StateSchema, T, ContextSchema>

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution
    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

      • StateSchema extends any = AnnotationRoot<{}>
      • T extends Record<string, any> = Record<string, any>
      • ContextSchema extends any = AnyAnnotationRoot

      Parameters

      Returns ReactAgent<StateSchema, T, ContextSchema>

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution
    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

      • StateSchema extends any = AnnotationRoot<{}>
      • ContextSchema extends any = AnyAnnotationRoot

      Parameters

      • params: Omit<
            CreateAgentParams<
                StateSchema,
                ResponseFormatUndefined,
                ContextSchema,
                never,
            >,
            "responseFormat",
        >

      Returns ReactAgent<StateSchema, ResponseFormatUndefined, ContextSchema>

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution
    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

      • StateSchema extends any = AnnotationRoot<{}>
      • ContextSchema extends any = AnyAnnotationRoot

      Parameters

      • params: Omit<
            CreateAgentParams<
                StateSchema,
                ResponseFormatUndefined,
                ContextSchema,
                never,
            >,
            "responseFormat",
        > & { responseFormat?: undefined }

      Returns ReactAgent<StateSchema, ResponseFormatUndefined, ContextSchema>

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution
    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

      • StateSchema extends any = AnnotationRoot<{}>
      • StructuredResponseFormat extends Record<string, any> = Record<string, any>
      • ContextSchema extends any = AnyAnnotationRoot

      Parameters

      Returns ReactAgent<StateSchema, StructuredResponseFormat, ContextSchema>

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution
    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

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

      Parameters

      • params: CreateAgentParams<T, ContextSchema, InteropZodType<T>> & {
            middleware: TMiddleware;
            responseFormat: InteropZodType<T>;
        }

      Returns ReactAgent<T, ContextSchema, TMiddleware>

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution
    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

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

      Parameters

      • params: CreateAgentParams<
            ExtractZodArrayTypes<T> extends Record<string, any>
                ? ExtractZodArrayTypes<ExtractZodArrayTypes<T>>
                : Record<string, any>,
            ContextSchema,
            T,
        > & { middleware: TMiddleware; responseFormat: T }

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

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution
    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

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

      Parameters

      • params: CreateAgentParams<Record<string, unknown>, ContextSchema, JsonSchemaFormat> & {
            middleware: TMiddleware;
            responseFormat: JsonSchemaFormat;
        }

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

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution
    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

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

      Parameters

      • params: CreateAgentParams<Record<string, unknown>, ContextSchema, JsonSchemaFormat[]> & {
            middleware: TMiddleware;
            responseFormat: JsonSchemaFormat[];
        }

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

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution
    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

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

      Parameters

      • params: CreateAgentParams<
            Record<string, unknown>,
            ContextSchema,
            JsonSchemaFormat | JsonSchemaFormat[],
        > & {
            middleware: TMiddleware;
            responseFormat: JsonSchemaFormat | JsonSchemaFormat[];
        }

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

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution
    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

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

      Parameters

      • params: CreateAgentParams<T, ContextSchema, TypedToolStrategy<T>> & {
            middleware: TMiddleware;
            responseFormat: TypedToolStrategy<T>;
        }

      Returns ReactAgent<T, ContextSchema, TMiddleware>

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution
    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

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

      Parameters

      • params: CreateAgentParams<T, ContextSchema, ToolStrategy<T>> & {
            middleware: TMiddleware;
            responseFormat: ToolStrategy<T>;
        }

      Returns ReactAgent<T, ContextSchema, TMiddleware>

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution
    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

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

      Parameters

      • params: CreateAgentParams<T, ContextSchema, ProviderStrategy<T>> & {
            middleware: TMiddleware;
            responseFormat: ProviderStrategy<T>;
        }

      Returns ReactAgent<T, ContextSchema, TMiddleware>

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution
    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

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

      Parameters

      • params: Omit<
            CreateAgentParams<ResponseFormatUndefined, ContextSchema, never>,
            "responseFormat",
        > & { middleware: TMiddleware }

      Returns ReactAgent<ResponseFormatUndefined, ContextSchema, TMiddleware>

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution
    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

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

      Parameters

      • params: Omit<
            CreateAgentParams<ResponseFormatUndefined, ContextSchema, never>,
            "responseFormat",
        > & { middleware: TMiddleware; responseFormat?: undefined }

      Returns ReactAgent<ResponseFormatUndefined, ContextSchema, TMiddleware>

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution
    • 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 ReAct (Reasoning + Acting) agent implementation based on the paper Synergizing Reasoning and Acting in Language Models.

      Type Parameters

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

      Parameters

      Returns ReactAgent<StructuredResponseFormat, ContextSchema, TMiddleware>

      import { createAgent, tool } from "langchain";
      import { z } from "zod/v3";

      const getWeather = tool((input) => {
      if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
      return "It's 60 degrees and foggy.";
      } else {
      return "It's 90 degrees and sunny.";
      }
      }, {
      name: "get_weather",
      description: "Call to get the current weather.",
      schema: z.object({
      location: z.string().describe("Location to get the weather for."),
      })
      })

      const agent = createAgent({
      // use chat model from "@langchain/openai"
      model: "openai:gpt-4o-mini",
      tools: [getWeather]
      });

      const inputs = {
      messages: [{ role: "user", content: "what is the weather in SF?" }],
      };

      const stream = await agent.stream(inputs, { streamMode: "values" });

      for await (const { messages } of stream) {
      console.log(messages);
      }
      // Returns the messages in the state at each step of execution