Overview

The situation that fits middleware Tracking agent behavior with logging, analytics, and debugging. Transforming prompts, tool selection, and output formatting. Adding retries, fallbacks, and early termination logic. Applying rate limits, guardrails, and PII detection. The agent loop the core agent loop involves calling a model ,letting it choose tools to execute,and then finishing when it calls no more tools Middleware在其中的作用: exposes hooks before and after each of these steps ...

FrameworksLangChain
1 分钟246 字chenghaoning51

Long-term-memory

Across sessions and conversations Long-term memory 建立在Lang Graph stores,存储的形式:JSON documents organized by namespace and key Usage 为了给一个agent添加memory功能,在使用create_agent时create a store and pass it to agent ...

FrameworksLangChain
2 分钟846 字chenghaoning51

Models

In addition to text generation,many LLMs provide :tool calling,Structured output,Multimodality,Reasoning different model excel at different tasks LangChain has Standard model interface,which allow you to experiment with and switch models Basic usage 1.with agents 2.Standalone(outside the agent loop) Initialize a model the easiest way: use init_chat_model 1 2 3 4 5 6 import os from langchain.chat_models import init_chat_model os.environ["OPENAI_API_KEY"] = "sk-..." model = init_chat_model("gpt-5.5") key method: invoke,streaming.patch(批量处理) ...

FrameworksLangChain
4 分钟1543 字chenghaoning51

Open-SWE

Convention part 1.使用async方法,只在必须的时候才使用sync方法(不是通用的设计范式): 约束之一为:Use async-only implementations. Add a sync method only when an interface requires it, and then raise NotImplementedError. 1.使用async方法优化并发性能 2.使用NotImplementedError显式告知为提供async实现,采用同步方法 ...

FrameworksLangChain
5 分钟2482 字chenghaoning51

Prebuilt Middleware

Tool error 捕获工具异常,并且转化为ToolMessage,模型收到信息并且从中修复 API reference: ToolErrorMiddleware 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 from langchain.agents import create_agent from langchain.agents.middleware import ToolErrorMiddleware, ToolRetryMiddleware def on_error(exc: Exception, request: ToolCallRequest) -> str | None: # Surface ValueError to the model so it can correct the input if isinstance(exc, ValueError): return f"`{request.tool_call['name']}` failed: {type(exc).__name__}. Fix the input and retry." # Let all other exceptions propagate (halts the run) return None # Async-only usage async def aon_error(exc: Exception, request: ToolCallRequest) -> str | None: if isinstance(exc, ConnectionError): return f"Tool `{request.tool_call['name']}` encountered a connection error." return None agent = create_agent( model="gpt-5.5", tools=[search_tool, database_tool], middleware=[ # Place retry inner so exceptions reach ToolErrorMiddleware after retries are exhausted ToolRetryMiddleware(max_retries=3, on_failure="error"), ToolErrorMiddleware(on_error=on_error, tools=["search_tool"]), ], ) # Async-only: pass aon_error alone (do not pass on_error) async_agent = create_agent( model="gpt-5.5", tools=[api_tool], middleware=[ToolErrorMiddleware(aon_error=aon_error)], ) Tool retry 通过可配置的指数避退机制来自动重试失败tool calls ...

FrameworksLangChain
3 分钟1373 字chenghaoning51

Retrieval

Two limitations of LLM: finite context Static Knowledge RAG(检索增强生成) Building a knowledge base A repository of documents of structured data 使用LangChain’s document loaders and vector stores 来建立你自己的知识库 如果你已经有知识库,你不需要重建,你只需要 ...

FrameworksLangChain
1 分钟480 字chenghaoning51

Short-term Memory

Conversation history is the most common form of short-term memory Fact1: message includes instructions and inpus Usage To add short-term memory to an agent ,you need to specify a checkpointer when creating an agent note: short-term memory is part of your agent’s state. State is persisted in a database using a checkpointer so thread can resume it an any time. Short-term memory updates when the agent is invoked or a step (like a tool call) is completed, and the state is read at the start of each step. ...

FrameworksLangChain
3 分钟1481 字chenghaoning51

Runtime

LangChain’s create_agent的底层运行在LangGraph’s runtime LangGraph暴露一个Runtime object,提供以下的信息: 1.context:静态信息:user id,数据库连接和其他的依赖 2.Store:用于长期记忆的BaseStore实例 3.Stream writer:通过"custom"流式传输模式来硫化信息的object 4.Execution info: identity and retry information for the current execution(thread ID, run ID ,attmpt number) 5.Server info:在 LangGraph Server 上运行时的特定于服务器的元数据(assistant ID,graph ID, authenticated user ) You can access the runtime information within tools and middleware ...

FrameworksLangChain
2 分钟962 字chenghaoning51

Streaming

Overview 实现的功能: Stream agent progress Stream LLM tokens Streaming thinking/reasoning tokens Stream custom updates Stream multiple modes:从updates,messages,custom中进行选择 Supported stream modes 将下列的一个或者多个流式传输模式作为列表传输给stream或者astream方法 ...

FrameworksLangChain
5 分钟2472 字chenghaoning51

Structured Output

返回的形式: JSON objects, Pydantic models,or dataclasses that your application can use directly LangChain’s create_agent 会自动处理结构化输出 用户设置desired 结构化输出的schema 返回在 in the key strucured_responese key of the agent’s state Field函数的作用: 结构化输出时,是创建元数据的重要途径,且其内部的description可以作为Prompt的一部分指导LLM的工作 ...

FrameworksLangChain
4 分钟1930 字chenghaoning51