await tracer.trace(async (ctx) => {
const wrappedOpenAI = tracer.wrapOpenAI(openai, {
context: ctx,
trackToolCalls: true // Default: true
});
const response = await wrappedOpenAI.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'What is the weather in Paris?' }],
tools: [{
type: 'function',
function: {
name: 'get_weather',
parameters: {
type: 'object',
properties: {
location: { type: 'string' }
}
}
}
}]
});
// Tool calls captured in span
const toolCalls = response.choices[0].message.tool_calls;
if (toolCalls) {
for (const toolCall of toolCalls) {
// Execute the tool
const result = await executeFunction(
toolCall.function.name,
JSON.parse(toolCall.function.arguments)
);
// Continue conversation with tool result
// (also automatically traced)
}
}
});