import { Agent, BatchTraceProcessor, run, setTraceProcessors, withTrace } from '@openai/agents';import { KeywordsAIOpenAIAgentsTracingExporter } from '@keywordsai/exporter-openai-agents';setTraceProcessors([ new BatchTraceProcessor( new KeywordsAIOpenAIAgentsTracingExporter(), ),]);async function main() { const agent = new Agent({ name: 'Assistant', instructions: 'You only respond in haikus.', }); const result = await withTrace('Hello World', async () => { return run(agent, 'Tell me about recursion in programming.'); }); console.log(result.finalOutput);}main().catch(console.error);
Here’s a more comprehensive example with multiple agents:
full-example.ts
Copy
import { Agent, BatchTraceProcessor, run, setTraceProcessors, withTrace } from '@openai/agents';import { KeywordsAIOpenAIAgentsTracingExporter } from '@keywordsai/exporter-openai-agents';setTraceProcessors([ new BatchTraceProcessor( new KeywordsAIOpenAIAgentsTracingExporter(), ),]);const agent = new Agent({ model: "gpt-4o-mini", name: "Apple Agent", instructions: "You are a helpful assistant who knows about apples.",});const secondAgent = new Agent({ model: "gpt-4o-mini", name: "Banana Agent", instructions: "You are a helpful assistant who knows about bananas.",});async function main() { await withTrace("My Trace", async () => { const response = await run(agent, "Hello, what fruit do you like?"); console.log(response.finalOutput); const secondResponse = await run(secondAgent, "Hello, what fruit do you like?"); console.log(secondResponse.finalOutput); });}main().catch(console.error);
You can add custom metadata (properties) to your traces for better tracking and debugging:
metadata-example.ts
Copy
import { Agent, BatchTraceProcessor, run, setTraceProcessors, withTrace } from '@openai/agents';import { KeywordsAIOpenAIAgentsTracingExporter } from '@keywordsai/exporter-openai-agents';setTraceProcessors([ new BatchTraceProcessor( new KeywordsAIOpenAIAgentsTracingExporter(), ),]);const agent = new Agent({ model: "gpt-4o-mini", name: "Apple Agent", instructions: "You are a helpful assistant who knows about apples.",});const secondAgent = new Agent({ model: "gpt-4o-mini", name: "Banana Agent", instructions: "You are a helpful assistant who knows about bananas.",});async function main() { await withTrace("My Trace", async () => { const response = await run(agent, "Hello, what fruit do you like?"); console.log(response.finalOutput); const secondResponse = await run(secondAgent, "Hello, what fruit do you like?"); console.log(secondResponse.finalOutput); }, { metadata: { foo: "bar", } });}main().catch(console.error);
Agents can hand off conversations to other specialized agents based on context:
handoff.ts
Copy
import { Agent, BatchTraceProcessor, run, setTraceProcessors, withTrace } from '@openai/agents';import { KeywordsAIOpenAIAgentsTracingExporter } from '@keywordsai/exporter-openai-agents';setTraceProcessors([ new BatchTraceProcessor( new KeywordsAIOpenAIAgentsTracingExporter(), ),]);const spanish_agent = new Agent({ name: "Spanish agent", instructions: "You only speak Spanish.",});const english_agent = new Agent({ name: "English agent", instructions: "You only speak English",});const triage_agent = new Agent({ name: "Triage agent", instructions: "Handoff to the appropriate agent based on the language of the request.", handoffs: [spanish_agent, english_agent],});async function main() { const result = await withTrace("Handoff Example", async () => { return run(triage_agent, "Hola, ¿cómo estás?"); }); console.log(result.finalOutput);}main().catch(console.error);
Run the example:
Copy
npx tsx handoff.ts
The resulting trace root span will have the custom properties visible in the Keywords AI platform: