> ## Documentation Index
> Fetch the complete documentation index at: https://docs.wandb.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Class: Tool

> TypeScript SDK reference

[weave](../) / Tool

A tool invocation. Emits an `execute_tool` span carrying the tool name,
the JSON-encoded arguments, the tool-call id, and the result.

Created by `weave.startTool()` (or `turn.startTool()`, or
`llm.startTool()`) and terminated with `end()`. Assign `result` before
calling `end()` to record the tool's output on the span.

`Example`

```ts twoslash theme={null}
// @noErrors
const tool = weave.startTool({
  name: tc.function.name,
  args: tc.function.arguments,
  toolCallId: tc.id,
});
try {
  tool.result = await wikipediaSearch(JSON.parse(tc.function.arguments));
} finally {
  tool.end();
}
```

## Hierarchy

* `SpanBase`

  ↳ `Tool`

## Table of contents

### Properties

* [args](./tool#args)
* [name](./tool#name)
* [result](./tool#result)
* [toolCallId](./tool#toolcallid)

### Methods

* [addEvent](./tool#addevent)
* [end](./tool#end)
* [setAttributes](./tool#setattributes)
* [create](./tool#create)

## Properties

### args

• `Readonly` **args**: `string`

#### Defined in

[src/genai/tool.ts:52](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/tool.ts#L52)

***

### name

• `Readonly` **name**: `string`

#### Defined in

[src/genai/tool.ts:51](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/tool.ts#L51)

***

### result

• `Optional` **result**: `string`

Tool output as a string. Recorded on `gen_ai.tool.call.result` at `end()`.

#### Defined in

[src/genai/tool.ts:47](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/tool.ts#L47)

***

### toolCallId

• `Readonly` **toolCallId**: `string`

#### Defined in

[src/genai/tool.ts:53](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/tool.ts#L53)

## Methods

### addEvent

▸ **addEvent**(`name`, `attributes?`, `startTime?`): `this`

Add a named event to the span. Useful for marking non-span moments such as
context compaction, tool-loop detection, or guardrail trips. Warns and
no-ops after `end()`. Mirrors OTel `Span.addEvent`.

#### Parameters

| Name          | Type         |
| :------------ | :----------- |
| `name`        | `string`     |
| `attributes?` | `Attributes` |
| `startTime?`  | `TimeInput`  |

#### Returns

`this`

`Example`

```ts twoslash theme={null}
// @noErrors
span.addEvent('context_compacted', {removedMessages: 12});
```

#### Inherited from

SpanBase.addEvent

#### Defined in

[src/genai/spanBase.ts:77](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/spanBase.ts#L77)

***

### end

▸ **end**(`opts?`): `void`

Flush `result` to the span and close it. Idempotent. Pass `error` to mark
the span as failed; pass `endTime` to backdate the close.

#### Parameters

| Name    | Type             |
| :------ | :--------------- |
| `opts?` | `SpanEndOptions` |

#### Returns

`void`

#### Defined in

[src/genai/tool.ts:87](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/tool.ts#L87)

***

### setAttributes

▸ **setAttributes**(`attributes`): `this`

Set multiple attributes on the span at once. Warns and no-ops after
`end()`. Mirrors OTel `Span.setAttributes` (and the Python SDK's
`set_attributes`).

#### Parameters

| Name         | Type         |
| :----------- | :----------- |
| `attributes` | `Attributes` |

#### Returns

`this`

`Example`

```ts twoslash theme={null}
// @noErrors
span.setAttributes({'weave.tag': 'prod', 'gen_ai.response.id': id});
```

#### Inherited from

SpanBase.setAttributes

#### Defined in

[src/genai/spanBase.ts:63](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/spanBase.ts#L63)

***

### create

▸ **create**(`opts`): [`Tool`](./tool)

#### Parameters

| Name   | Type                                                      |
| :----- | :-------------------------------------------------------- |
| `opts` | [`ToolInit`](../interfaces/toolinit) & `ChildSpanContext` |

#### Returns

[`Tool`](./tool)

#### Defined in

[src/genai/tool.ts:58](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/tool.ts#L58)
