9 月 20 日学习报告:LangChain Agent 的 Tool、Memory 与 Streaming

9 月 20 日学习报告:LangChain Agent 的 Tool、Memory 与 Streaming 仅整理 2026-09-20 修改的 4 个文件。文件按最后修改时间排列;各文件内部保持原文标题顺序与层级,只压缩重复表述和示例,不省略知识点。 ...

FrameworksLangChain
14 分钟6680 字chenghaoning51

LangChain MCP Overview

Model Context Protocol (MCP) is an open protocol that standardizes how applications provide tools and context to language models.

FrameworksLangChain
1 分钟19 字chenghaoning51

Event Streaming

LangChain agents are built on LangGraph, so they support the same streaming stack with agent-focused projections for messages, tool calls, state, and custom updates. For most situation,use Event Streaming throughstream_event(...,version="v3") Event Streaming returns a run object with typed projections, so each projection can be consumed independently instead of parsing stream-mode tuples 1 2 3 4 5 6 7 8 9 stream = agent.stream_events({ "messages": [{"role": "user", "content": "What is the weather in SF?"}], }, version="v3") for message in stream.messages: for delta in message.text: print(delta, end="", flush=True) final_state = stream.output What you can stream Projection Use for event in stream Raw protocol events with full envelope and access to every channel. stream.messages Model message streams, one per LLM call. message.text Text deltas and final text for a message. message.reasoning Reasoning deltas for models that expose reasoning content. message.tool_calls Tool-call argument chunks and finalized tool calls. message.output Final message object after the model call completes. stream.values Agent state snapshots. stream.output Final agent state. stream.subgraphs Nested graph runs (sub-agents and plain subgraphs). stream.extensions Custom transformer projections. stream.tool_calls Tool execution lifecycle, inputs, output deltas, final output, and errors. stream.messages 会生成 ChatModelStream 对象。每个消息流都暴露了 .text、.reasoning、.tool_calls 和 .output。同步投影可迭代以获取实时增量,也可排空以获取最终值:使用 str(message.text) 获取最终文本,使用 message.tool_calls.get() 获取已完成的工具调用 ...

FrameworksLangChain
4 分钟1674 字chenghaoning51

Context Engineering

Overview 构建agent的难点在于使他们足够可靠 即时他们在原型阶段往往会表现得很好 Why do agent fails LLM调用采取了错误的措施或者没有按照预期执行,原因: LLM能力不够 正确的context没有传递给LLM 更多情况是后者 ...

FrameworksLangChain
2 分钟927 字chenghaoning51

LangChain A Quick Start

一个简单的示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 from langchain.agents import create_agent def get_weather(city: str) -> str: """Get weather for a given city.""" return f"It's always sunny in {city}!" agent = create_agent( model="claude-sonnet-4-6", tools=[get_weather], system_prompt="You are a helpful assistant", ) result = agent.invoke( {"messages": [{"role": "user", "content": "What's the weather in San Francisco?"}]} ) print(result["messages"][-1].content_blocks) Build a real-world agent: Detailed System Prompt Create Tools Model Configuration Conversational memory Deep agents Testing 1.Define the system prompt defines your agent’s role and behavior. Keep it specific and actionable. ...

FrameworksLangChain
1 分钟449 字chenghaoning51

Custom Middleware

通过在agent工作流中实现hook来构建自定义的midddleware Hooks two types: Node-style hook 在特定的执行点顺序执行.Use for validation,logging and state updates. Hook When it runs before_agent Before agent starts (once per invocation) before_model Before each model call after_model After each model response after_agent After agent completes (once per invocation) Wrap-style hooks run around each call, giving you control over execution: ...

FrameworksLangChain
7 分钟3256 字chenghaoning51

Guardrails

GuardRails通过validating和flitering content来帮助你构建安全合规的agent 常见的应用场景: 预防个人信息的泄露 检测和阻止提示词注入攻击 阻止不恰当和有害的攻击 执行业务规则和合规性要求 验证输出重量和正确性 你可以在使用middleware在关键节点拦截执行 before the agent starts, after it completes, or around model and tool calls. ...

FrameworksLangChain
3 分钟1313 字chenghaoning51

LangChain Philosophy

LangChain is driven by a few core beliefs: Large Language Models (LLMs) are great, powerful new technology. LLMs are even better when you combine them with external sources of data. LLMs will transform what the applications of the future look like. Specifically, the applications of the future will look more and more agentic. It is still very early on in that transformation. While it’s easy to build a prototype of those agentic applications, it’s still really hard to build agents that are reliable enough to put into production.

FrameworksLangChain
1 分钟94 字chenghaoning51

DeepFlow

1.langgraph.json文件的作用 when: 使用Langgraph cli部署本地API服务 部署到langgraph cloud/langsmith deployment 使用langgraph studio及逆行调试 不需要: 使用传统的后端API服务;纯脚本/嵌入式开发 ...

FrameworksLangChain
2 分钟519 字chenghaoning51

Human-in-the-loop

By checking each tool call against a configurable policy 如果需要暂停,middleware将会issue an interrupt来暂停执行 图状态将会使用LangGraph的persistence layer来保存,因此可以pause and resume later ...

FrameworksLangChain
3 分钟1412 字chenghaoning51