Skip to main content
A single agent system involves one autonomous entity that perceives its environment and takes actions to achieve specific goals. It makes decisions independently, without coordination or interaction with other agents.

Installation

npm i langchain @langchain/openai zod

Quick Start

Create your first agent:
import { createAgent } from "langchain";
import { ChatOpenAI } from "@langchain/openai";

const model = new ChatOpenAI({
  model: process.env.OPENAI_MODEL || "gpt-4o-mini",
  apiKey: process.env.OPENAI_API_KEY,
  configuration: {
    baseURL: process.env.OPENAI_BASE_URL,
  },
});

const agent = createAgent({
  model,
});

await agent.invoke({
  messages: [{ role: "user", content: "Hello, how are you?" }],
});

Adding Tools

Tools extend agent capabilities beyond text generation.
import { createAgent, tool } from "langchain";
import { ChatOpenAI } from "@langchain/openai";
import * as z from "zod";

const getWeather = tool((input) => `It's always sunny in ${input.city}!`, {
  name: "get_weather",
  description: "Get the weather for a given city",
  schema: z.object({
    city: z.string().describe("The city to get the weather for"),
  }),
});

const model = new ChatOpenAI({
  model: process.env.OPENAI_MODEL || "gpt-4o-mini",
  apiKey: process.env.OPENAI_API_KEY,
  configuration: {
    baseURL: process.env.OPENAI_BASE_URL,
  },
});

const agent = createAgent({
  model,
  tools: [getWeather],
});

await agent.invoke({
  messages: [{ role: "user", content: "What's the weather in Tokyo?" }],
});

Next Steps