Building a LangChain Agent with Tools, Structured Output, and Streaming
A tutorial explains how to build an assistant with LangChain’s create_agent. It covers tool definitions, automatic tool-call loops, runtime context, schema-based structured output, retries for invalid structured responses, and streaming events. The article describes create_agent as LangChain’s minimal Harness implementation and concludes with an example combining multiple tools, structured output, and streaming.
The tutorial shows developers how to assemble a runnable LangChain assistant without manually implementing the tool-calling loop or constructing ToolMessage objects.
LangChain Agent智能助手构建笔记:创建、工具调用、结构化输出与流式输出
1. 智能助手的核心公式
LangChain 官方将 Agent 定义为 “模型 + Harness(执行框架)”。Harness 是模型循环之外的一切:提示词、工具、中间件、状态管理。create_agent 就是这个 Harness 的最小实现,让你用最少的代码组装出一个可运行的智能助手[citation:1][citation:9]。
一个智能助手的最小闭环是:用户输入 → 模型决策 → 调用工具(如果需要)→ 结果回传 → 模型组织最终回复。你之前学过的 Tool Calling 闭环、Message 流转、结构化输出,都在这个循环里各司其职。