import z from "zod";import { tool, createAgent } from "langchain";import { LangGraphRunnableConfig } from "@langchain/langgraph";const getWeather = tool( async (input, config: LangGraphRunnableConfig) => { // Stream any arbitrary data config.writer?.(`Looking up data for city: ${input.city}`); // ... fetch city data config.writer?.(`Acquired data for city: ${input.city}`); return `It's always sunny in ${input.city}!`; }, { name: "get_weather", description: "Get weather for a given city.", schema: z.object({ city: z.string().describe("The city to get weather for."), }), });const agent = createAgent({ model: "gpt-4.1-mini", tools: [getWeather],});for await (const chunk of await agent.stream( { messages: [{ role: "user", content: "what is the weather in sf" }] }, { streamMode: "custom" })) { console.log(chunk);}
Output
Copy
Looking up data for city: San FranciscoAcquired data for city: San Francisco
import z from "zod";import { tool, createAgent } from "langchain";import { LangGraphRunnableConfig } from "@langchain/langgraph";const getWeather = tool( async (input, config: LangGraphRunnableConfig) => { // Stream any arbitrary data config.writer?.(`Looking up data for city: ${input.city}`); // ... fetch city data config.writer?.(`Acquired data for city: ${input.city}`); return `It's always sunny in ${input.city}!`; }, { name: "get_weather", description: "Get weather for a given city.", schema: z.object({ city: z.string().describe("The city to get weather for."), }), });const agent = createAgent({ model: "gpt-4.1-mini", tools: [getWeather],});for await (const [streamMode, chunk] of await agent.stream( { messages: [{ role: "user", content: "what is the weather in sf" }] }, { streamMode: ["updates", "messages", "custom"] })) { console.log(`${streamMode}: ${JSON.stringify(chunk, null, 2)}`);}