withTool()

Wrap an async function as a traced tool

Overview

Use withTool(options, fn) to trace tool or function calls within agents. Tools represent specific capabilities that agents can invoke.

Signature

1withTool<T>(
2 options: {
3 name: string;
4 version?: number;
5 associationProperties?: Record<string, any>;
6 },
7 fn: () => Promise<T>
8): Promise<T>

Basic Usage

1import { RespanTelemetry } from '@respan/tracing';
2
3const respanAi = new RespanTelemetry({
4 apiKey: process.env.RESPAN_API_KEY,
5 appName: 'my-app'
6});
7
8await respanAi.initialize();
9
10const result = await respanAi.withTool(
11 { name: 'web_search' },
12 async () => {
13 return await searchWeb('latest AI news');
14 }
15);

Tools within an Agent

1await respanAi.withAgent(
2 { name: 'research_agent' },
3 async () => {
4 // Tool 1: Web search
5 const searchResults = await respanAi.withTool(
6 {
7 name: 'web_search',
8 associationProperties: {
9 'query': 'machine learning trends',
10 'source': 'google'
11 }
12 },
13 async () => {
14 return await searchWeb('machine learning trends');
15 }
16 );
17
18 // Tool 2: Summarize
19 const summary = await respanAi.withTool(
20 { name: 'summarizer' },
21 async () => {
22 return await summarizeText(searchResults);
23 }
24 );
25
26 // Tool 3: Format response
27 const formatted = await respanAi.withTool(
28 { name: 'formatter' },
29 async () => {
30 return await formatMarkdown(summary);
31 }
32 );
33
34 return formatted;
35 }
36);

API Integration Tool

1await respanAi.withTool(
2 {
3 name: 'weather_api',
4 associationProperties: {
5 'location': 'San Francisco',
6 'units': 'metric'
7 }
8 },
9 async () => {
10 const response = await fetch(
11 'https://api.weather.com/v1/forecast?location=SF'
12 );
13 return await response.json();
14 }
15);

Database Query Tool

1await respanAi.withTool(
2 {
3 name: 'database_query',
4 associationProperties: {
5 'table': 'users',
6 'operation': 'select'
7 }
8 },
9 async () => {
10 return await db.query('SELECT * FROM users WHERE active = true');
11 }
12);

Custom Calculator Tool

1const calculator = async (operation: string, a: number, b: number) => {
2 return await respanAi.withTool(
3 {
4 name: 'calculator',
5 associationProperties: {
6 'operation': operation,
7 'inputs': [a, b]
8 }
9 },
10 async () => {
11 switch (operation) {
12 case 'add': return a + b;
13 case 'subtract': return a - b;
14 case 'multiply': return a * b;
15 case 'divide': return a / b;
16 default: throw new Error('Invalid operation');
17 }
18 }
19 );
20};
21
22// Usage in agent
23await respanAi.withAgent(
24 { name: 'math_agent' },
25 async () => {
26 const sum = await calculator('add', 10, 5);
27 const product = await calculator('multiply', sum, 2);
28 return product;
29 }
30);

Parameters

name
stringRequired

Tool display name for identification in the Respan dashboard

version
number

Version number for tracking tool iterations

associationProperties
Record<string, any>

Custom metadata to associate with the tool (inputs, configuration, etc.)

Return Value

Returns a Promise that resolves to the return value of the provided function.

Best Practices

  • Use tools for specific capabilities that agents can invoke
  • Name tools clearly to reflect their function
  • Always nest tools within agents for proper hierarchy
  • Add association properties to track tool inputs and configuration
  • Tools are automatically linked to their parent agent in traces
  • Tools can be reused across multiple agents