Skip to content

[AI-SDK] How to trace runs from new Agent class of v5 (and ToolLoopAgent in v6) #1073

@arthberman

Description

@arthberman

wrapAISDK seems to not support new Agent class from v5 of ai-sdk (that will be ToolLoopAgent class in v6)
Any guidance on how to trace it properly with Braintrust? I currently wrap my calls with traced() to group them in the same trace, so I don't know how the OTEL approach can help me as I need to group LLM runs within the same trace

Example of new Agent class usage:

import { Experimental_Agent as Agent, stepCountIs, tool } from 'ai';
import { z } from 'zod';

const weatherAgent = new Agent({
  model: 'openai/gpt-4o',
  tools: {
    weather: tool({
      description: 'Get the weather in a location (in Fahrenheit)',
      inputSchema: z.object({
        location: z.string().describe('The location to get the weather for'),
      }),
      execute: async ({ location }) => ({
        location,
        temperature: 72 + Math.floor(Math.random() * 21) - 10,
      }),
    }),
    convertFahrenheitToCelsius: tool({
      description: 'Convert temperature from Fahrenheit to Celsius',
      inputSchema: z.object({
        temperature: z.number().describe('Temperature in Fahrenheit'),
      }),
      execute: async ({ temperature }) => {
        const celsius = Math.round((temperature - 32) * (5 / 9));
        return { celsius };
      },
    }),
  },
  stopWhen: stepCountIs(20),
});

const result = await weatherAgent.generate({
  prompt: 'What is the weather in San Francisco in celsius?',
});

console.log(result.text); // agent's final answer
console.log(result.steps); // steps taken by the agent

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions