Use this file to discover all available pages before exploring further.
This quickstart takes you from a simple setup to a fully functional AI agent in just a few minutes.
LangChain Docs MCP serverIf you’re using an AI coding assistant or IDE (e.g. Claude Code or Cursor), you should install the LangChain Docs MCP server to get the most out of it. This ensures your agent has access to up-to-date LangChain documentation and examples.
Start by creating a simple agent that can answer questions and call tools. The agent will use Claude Sonnet 4.5 as its language model, a basic weather function as a tool, and a simple prompt to guide its behavior.
import { createAgent, tool } from "langchain";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 agent = createAgent({ model: "claude-sonnet-4-5-20250929", tools: [getWeather],});console.log( await agent.invoke({ messages: [{ role: "user", content: "What's the weather in Tokyo?" }], }));
Next, build a practical weather forecasting agent that demonstrates key production concepts:
Detailed system prompts for better agent behavior
Create tools that integrate with external data
Model configuration for consistent responses
Structured output for predictable results
Conversational memory for chat-like interactions
Create and run the agent to test the fully functional agent
Let’s walk through each step:
1
Define the system prompt
The system prompt defines your agent’s role and behavior. Keep it specific and actionable:
const systemPrompt = `You are an expert weather forecaster, who speaks in puns.You have access to two tools:- get_weather_for_location: use this to get the weather for a specific location- get_user_location: use this to get the user's locationIf a user asks you for the weather, make sure you know the location. If you can tell from the question that they mean wherever they are, use the get_user_location tool to find their location.`;
2
Create tools
Tools are functions your agent can call. Oftentimes tools will want to connect to external systems, and will rely on runtime configuration to do so. Notice here how the getUserLocation tool does exactly that:
import { tool, type ToolRuntime } from "langchain";import * as z from "zod";const getWeather = tool( (input) => `It's always sunny in ${input.city}!`, { name: "get_weather_for_location", description: "Get the weather for a given city", schema: z.object({ city: z.string().describe("The city to get the weather for"), }), });type AgentRuntime = ToolRuntime<unknown, { user_id: string }>;const getUserLocation = tool( (_, config: AgentRuntime) => { const { user_id } = config.context; return user_id === "1" ? "Florida" : "SF"; }, { name: "get_user_location", description: "Retrieve user information based on user ID", });
Zod is a library for validating and parsing pre-defined schemas. You can use it to define the input schema for your tools to make sure the agent only calls the tool with the correct arguments.Alternatively, you can define the schema property as a JSON schema object. Keep in mind that JSON schemas won’t be validated at runtime.
Example: Using JSON schema for tool input
const getWeather = tool( ({ city }) => `It's always sunny in ${city}!`, { name: "get_weather_for_location", description: "Get the weather for a given city", schema: { type: "object", properties: { city: { type: "string", description: "The city to get the weather for" } }, required: ["city"] }, });
3
Configure your model
Set up your language model with the right parameters for your use case:
import { initChatModel } from "langchain";const model = await initChatModel( "claude-sonnet-4-5-20250929", { temperature: 0.5, timeout: 10, maxTokens: 1000 });
Depending on the model and provider chosen, initialization parameters may vary; refer to their reference pages for details.
4
Define response format
Optionally, define a structured response format if you need the agent responses to match
a specific schema.
Add memory to your agent to maintain state across interactions. This allows
the agent to remember previous conversations and context.
import { MemorySaver } from "@langchain/langgraph";const checkpointer = new MemorySaver();
In production, use a persistent checkpointer that saves message history to a database.
See Add and manage memory for more details.
6
Create and run the agent
Now assemble your agent with all the components and run it!
import { createAgent } from "langchain";const agent = createAgent({ model: "claude-sonnet-4-5-20250929", systemPrompt: systemPrompt, tools: [getUserLocation, getWeather], responseFormat, checkpointer,});// `thread_id` is a unique identifier for a given conversation.const config = { configurable: { thread_id: "1" }, context: { user_id: "1" },};const response = await agent.invoke( { messages: [{ role: "user", content: "what is the weather outside?" }] }, config);console.log(response.structuredResponse);// {// punny_response: "Florida is still having a 'sun-derful' day ...",// weather_conditions: "It's always sunny in Florida!"// }// Note that we can continue the conversation using the same `thread_id`.const thankYouResponse = await agent.invoke( { messages: [{ role: "user", content: "thank you!" }] }, config);console.log(thankYouResponse.structuredResponse);// {// punny_response: "You're 'thund-erfully' welcome! ...",// weather_conditions: undefined// }
Show Full example code
import { createAgent, tool, initChatModel, type ToolRuntime } from "langchain";import { MemorySaver } from "@langchain/langgraph";import * as z from "zod";// Define system promptconst systemPrompt = `You are an expert weather forecaster, who speaks in puns.You have access to two tools:- get_weather_for_location: use this to get the weather for a specific location- get_user_location: use this to get the user's locationIf a user asks you for the weather, make sure you know the location. If you can tell from the question that they mean wherever they are, use the get_user_location tool to find their location.`;// Define toolsconst getWeather = tool( ({ city }) => `It's always sunny in ${city}!`, { name: "get_weather_for_location", description: "Get the weather for a given city", schema: z.object({ city: z.string(), }), });type AgentRuntime = ToolRuntime<unknown, { user_id: string }>;const getUserLocation = tool( (_, config: AgentRuntime) => { const { user_id } = config.context; return user_id === "1" ? "Florida" : "SF"; }, { name: "get_user_location", description: "Retrieve user information based on user ID", schema: z.object({}), });// Configure modelconst model = await initChatModel( "claude-sonnet-4-5-20250929", { temperature: 0 });// Define response formatconst responseFormat = z.object({ punny_response: z.string(), weather_conditions: z.string().optional(),});// Set up memoryconst checkpointer = new MemorySaver();// Create agentconst agent = createAgent({ model, systemPrompt, responseFormat, checkpointer, tools: [getUserLocation, getWeather],});// Run agent// `thread_id` is a unique identifier for a given conversation.const config = { configurable: { thread_id: "1" }, context: { user_id: "1" },};const response = await agent.invoke( { messages: [{ role: "user", content: "what is the weather outside?" }] }, config);console.log(response.structuredResponse);// {// punny_response: "Florida is still having a 'sun-derful' day! The sunshine is playing 'ray-dio' hits all day long! I'd say it's the perfect weather for some 'solar-bration'! If you were hoping for rain, I'm afraid that idea is all 'washed up' - the forecast remains 'clear-ly' brilliant!",// weather_conditions: "It's always sunny in Florida!"// }// Note that we can continue the conversation using the same `thread_id`.const thankYouResponse = await agent.invoke( { messages: [{ role: "user", content: "thank you!" }] }, config);console.log(thankYouResponse.structuredResponse);// {// punny_response: "You're 'thund-erfully' welcome! It's always a 'breeze' to help you stay 'current' with the weather. I'm just 'cloud'-ing around waiting to 'shower' you with more forecasts whenever you need them. Have a 'sun-sational' day in the Florida sunshine!",// weather_conditions: undefined// }