// What does LangChain actually do? // Teaching excerpts; see the explanation and boundaries on the episode page. // Define the tool contract import { createAgent, tool, modelCallLimitMiddleware } from "langchain"; import { z } from "zod"; const orderStatus = tool( ({ orderId }) => readMyOrder(orderId), { name: "order_status", description: "Read my order status", schema: z.object({ orderId: z.string() }), } ); // Keep data access in your server function const demoOrders = new Map([ ["42", { owner: "demo-user", status: "In the kitchen" }], ["43", { owner: "another-user", status: "Out for delivery" }], ]); function readMyOrder(orderId, signedInUser = "demo-user") { const order = demoOrders.get(orderId); if (!order || order.owner !== signedInUser) { throw new Error("Order unavailable"); } return order.status; } // Connect the model and bound the loop const agent = createAgent({ model, tools: [orderStatus], middleware: [ modelCallLimitMiddleware({ runLimit: 3, exitBehavior: "error" }), ], }); const result = await agent.invoke({ messages: [{ role: "user", content: "Where is order 42?" }], }); const answer = result.messages.at(-1).content; // Choose the smallest useful flow // Chat-model invocation without an agent loop. const reply = await model.invoke("Say hello.");