LangChain 的 createAgent 在底层运行于 LangGraph 的运行时之上。
LangGraph 暴露了一个 Runtime 对象,其中包含以下信息:
Context (上下文) :静态信息,如用户 ID、数据库连接,或代理调用的其他依赖项
Store (存储) :一个 BaseStore 实例,用于 长期记忆
Stream writer (流写入器) :一个对象,用于通过 "custom" 流模式流式传输信息
运行时上下文是您将数据穿针引线通过代理的方式。您可以将值(如数据库连接、用户会话或配置)附加到上下文,并在工具和中间件中访问它们,而不是将事物存储在全局状态中。这保持了事物的无状态、可测试和可重用性。
您可以在 工具 和 中间件 中访问运行时信息。
使用 createAgent 创建代理时,您可以指定 contextSchema 来定义存储在代理 Runtime 中的 context 结构。
调用代理时,传递带有相关运行配置的 context 参数:
import * as z from "zod" ;
import { createAgent } from "langchain" ;
const contextSchema = z . object ( {
userName : z . string () ,
} ) ;
const agent = createAgent ( {
model : "gpt-4.1" ,
tools : [
/* ... */
] ,
contextSchema ,
} ) ;
const result = await agent . invoke (
{ messages : [ { role : "user" , content : "What's my name?" } ] },
{ context : { userName : "John Smith" } }
) ;
在工具内部
您可以在工具内部访问运行时信息以:
访问上下文
读取或写入长期记忆
写入 自定义流 (例如,工具进度/更新)
使用 runtime 参数在工具内部访问 Runtime 对象。
import * as z from "zod" ;
import { tool } from "langchain" ;
import { type ToolRuntime } from "@langchain/core/tools" ;
const contextSchema = z . object ( {
userName : z . string () ,
} ) ;
const fetchUserEmailPreferences = tool (
async ( _ , runtime : ToolRuntime < any , typeof contextSchema > ) => {
const userName = runtime . context ?. userName ;
if ( ! userName) {
throw new Error ( "userName is required" ) ;
}
let preferences = "The user prefers you to write a brief and polite email." ;
if (runtime . store) {
const memory = await runtime . store ?. get ([ "users" ] , userName) ;
if (memory) {
preferences = memory . value . preferences ;
}
}
return preferences ;
},
{
name : "fetch_user_email_preferences" ,
description : "Fetch the user's email preferences." ,
schema : z . object ( {} ) ,
}
) ;
在中间件内部
您可以在中间件中访问运行时信息,以根据用户上下文创建动态提示、修改消息或控制代理行为。
使用 runtime 参数在中间件内部访问 Runtime 对象。
import * as z from "zod" ;
import { createAgent , createMiddleware , SystemMessage } from "langchain" ;
const contextSchema = z . object ( {
userName : z . string () ,
} ) ;
// Dynamic prompt middleware
const dynamicPromptMiddleware = createMiddleware ( {
name : "DynamicPrompt" ,
contextSchema ,
beforeModel : ( state , runtime ) => {
const userName = runtime . context ?. userName ;
if ( ! userName) {
throw new Error ( "userName is required" ) ;
}
const systemMsg = `You are a helpful assistant. Address the user as ${ userName } .` ;
return {
messages : [ new SystemMessage (systemMsg) , ... state . messages] ,
};
},
} ) ;
// Logging middleware
const loggingMiddleware = createMiddleware ( {
name : "Logging" ,
contextSchema ,
beforeModel : ( state , runtime ) => {
console . log ( `Processing request for user: ${ runtime . context ?. userName } ` ) ;
return ;
},
afterModel : ( state , runtime ) => {
console . log ( `Completed request for user: ${ runtime . context ?. userName } ` ) ;
return ;
},
} ) ;
const agent = createAgent ( {
model : "gpt-4.1" ,
tools : [
/* ... */
] ,
middleware : [dynamicPromptMiddleware , loggingMiddleware] ,
contextSchema ,
} ) ;
const result = await agent . invoke (
{ messages : [ { role : "user" , content : "What's my name?" } ] },
{ context : { userName : "John Smith" } }
) ;
将这些文档连接 到 Claude、VSCode 等,通过 MCP 获取实时解答。