OpenAI Agents SDK 让你能够构建由 OpenAI 模型驱动的代理应用。
使用 LangSmith 追踪 OpenAI Agents SDK 运行,包括代理步骤、模型调用、工具调用和交接。
Python
安装
安装支持 OpenAI Agents 的 LangSmith:
pip install "langsmith[openai-agents]"
uv add "langsmith[openai-agents]"
这将同时安装 LangSmith 库和 OpenAI Agents SDK。
环境配置
# Optional: set a project for your traces
# For LangSmith API keys linked to multiple workspaces, set the LANGSMITH_WORKSPACE_ID environment variable to specify which workspace to use.
快速入门
通过使用 OpenAIAgentsTracingProcessor class.
from agents import Agent, Runner, set_trace_processors
from langsmith.integrations.openai_agents_sdk import OpenAIAgentsTracingProcessor
async def main():
agent = Agent(
name="Captain Obvious",
instructions="You are Captain Obvious, the world's most literal technical support agent.",
)
question = "Why is my code failing when I try to divide by zero? I keep getting this error message."
result = await Runner.run(agent, question)
print(result.final_output)
if __name__ == "__main__":
set_trace_processors([OpenAIAgentsTracingProcessor()])
asyncio.run(main())
代理的执行流程(包括跨度及其详情)会被记录到 LangSmith。
JavaScript
安装
安装 LangSmith 和 OpenAI Agents SDK:
npm install langsmith @openai/agents zod
yarn add langsmith @openai/agents zod
pnpm add langsmith @openai/agents zod
环境配置
# Optional: set a project for your traces
# For LangSmith API keys linked to multiple workspaces, set the LANGSMITH_WORKSPACE_ID environment variable to specify which workspace to use.
快速入门
在运行代理之前,向 OpenAI Agents SDK 注册 OpenAIAgentsTracingProcessor 。
setTraceProcessors([new OpenAIAgentsTracingProcessor()]);
const getWeather = tool({
name: "get_weather",
description: "Get the current weather for a city",
parameters: z.object({
city: z.string().describe("The city to get weather for"),
}),
execute: async ({ city }: { city: string }) => {
return `The weather in ${city} is sunny.`;
},
});
const agent = new Agent({
name: "WeatherAgent",
instructions: "You are a helpful assistant. Use the get_weather tool when asked about weather.",
model: "gpt-5-nano",
tools: [getWeather],
});
const result = await run(agent, "What's the weather in San Francisco?");
console.log(result.finalOutput);
生成的追踪包含根代理运行、响应跨度以及嵌套的工具调用跨度。
配置处理器
向处理器传递选项以设置 LangSmith 客户端、项目、标签、元数据或根追踪名称。
const client = new Client();
const processor = new OpenAIAgentsTracingProcessor({
client,
projectName: "openai-agents-demo",
name: "Support agent workflow",
tags: ["openai-agents"],
metadata: {
environment: "development",
},
});
setTraceProcessors([processor]);
const agent = new Agent({
name: "SupportAgent",
instructions: "You are a concise support agent.",
model: "gpt-5-nano",
});
const result = await run(agent, "Help me reset my password.");
console.log(result.finalOutput);
在工具中嵌套 traceable 调用
你可以在 OpenAI Agents SDK 工具处理器中使用 traceable 。LangSmith 会将这些运行嵌套在活动的工具跨度下。
setTraceProcessors([new OpenAIAgentsTracingProcessor()]);
const lookupOrder = traceable(
async (orderId: string) => {
return { orderId, status: "shipped" };
},
{ name: "lookup_order" }
);
const orderStatus = tool({
name: "order_status",
description: "Look up the status of an order",
parameters: z.object({
orderId: z.string().describe("The order ID to look up"),
}),
execute: async ({ orderId }: { orderId: string }) => {
return JSON.stringify(await lookupOrder(orderId));
},
});
const agent = new Agent({
name: "OrdersAgent",
instructions: "Use the order_status tool to answer order questions.",
model: "gpt-5-nano",
tools: [orderStatus],
});
await run(agent, "Where is order 123?");
在无服务器环境中刷新追踪
在无服务器环境中追踪时,请在进程退出前刷新待处理的追踪。
const client = new Client();
const processor = new OpenAIAgentsTracingProcessor({ client });
setTraceProcessors([processor]);
try {
const agent = new Agent({
name: "SupportAgent",
instructions: "You are a concise support agent.",
model: "gpt-5-nano",
});
const result = await run(agent, "Help me reset my password.");
console.log(result.finalOutput);
} finally {
await processor.forceFlush();
}