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-5.4-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);}
输出
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-5.4-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)}`);}
import z from "zod";import { createAgent, tool } from "langchain";import { ChatAnthropic } from "@langchain/anthropic";const getWeather = tool( async ({ city }) => { return `It's always sunny in ${city}!`; }, { name: "get_weather", description: "Get weather for a given city.", schema: z.object({ city: z.string() }), },);const agent = createAgent({ model: new ChatAnthropic({ model: "claude-sonnet-4-6", thinking: { type: "enabled", budget_tokens: 5000 }, }), tools: [getWeather],});for await (const [token, metadata] of await agent.stream( { messages: [{ role: "user", content: "What is the weather in SF?" }] }, { streamMode: "messages" },)) { if (!token.contentBlocks) continue; const reasoning = token.contentBlocks.filter((b) => b.type === "reasoning"); const text = token.contentBlocks.filter((b) => b.type === "text"); if (reasoning.length) { process.stdout.write(`[thinking] ${reasoning[0].reasoning}`); } if (text.length) { process.stdout.write(text[0].text); }}
输出
[thinking] The user is asking about the weather in San Francisco. I have a tool[thinking] available to get this information. Let me call the get_weather tool[thinking] with "San Francisco" as the city parameter.The weather in San Francisco is: It's always sunny in San Francisco!