> ## 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: Turn

> TypeScript SDK reference

[weave](../) / Turn

An agent invocation. Typically wraps the work to respond to a single
user message. Emits an `invoke_agent` span and acts as the root of the
trace for that turn: it is always started under `ROOT_CONTEXT` so it
never accidentally inherits a parent from another OTel-instrumented
library.

Created by `weave.startTurn()` (or `conversation.startTurn()`) and
terminated with `end()`. Only one Turn may be active in an async chain.
Children (LLM, Tool, SubAgent) attach via the `startLLM`, `startTool`,
`startSubagent` methods.

`Example`

```ts twoslash theme={null}
// @noErrors
const turn = weave.startTurn({agentName: 'research-bot', model: MODEL});
try {
  const llm = turn.startLLM({model: MODEL, providerName: 'openai'});
  // ...
  llm.end();
} finally {
  turn.end();
}
```

## Hierarchy

* `SpanBase`

  ↳ `Turn`

## Table of contents

### Properties

* [agentName](./turn#agentname)
* [model](./turn#model)

### Methods

* [addEvent](./turn#addevent)
* [end](./turn#end)
* [setAttribute](./turn#setattribute)
* [setAttributes](./turn#setattributes)
* [startLLM](./turn#startllm)
* [startSubagent](./turn#startsubagent)
* [startTool](./turn#starttool)
* [create](./turn#create)

## Properties

### agentName

• `Readonly` **agentName**: `string`

#### Defined in

[src/genai/turn.ts:57](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/turn.ts#L57)

***

### model

• `Readonly` **model**: `string`

#### Defined in

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

## 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`

Close the Turn span. Idempotent. Pass `error` to mark it as failed; pass
`endTime` to backdate the close.

#### Parameters

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

#### Returns

`void`

#### Defined in

[src/genai/turn.ts:144](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/turn.ts#L144)

***

### setAttribute

<Warning>
  **Deprecated.** Use [setAttributes](./turn#setattributes) instead, which mirrors the Python
  SDK's `set_attributes` and OTel's `Span.setAttributes`. Retained as a thin
  alias so existing single-attribute callers keep working. Only `Turn`
  carries this — the other emitters never shipped a singular form.
</Warning>

▸ **setAttribute**(`key`, `value`): `this`

#### Parameters

| Name    | Type             |
| :------ | :--------------- |
| `key`   | `string`         |
| `value` | `AttributeValue` |

#### Returns

`this`

#### Defined in

[src/genai/turn.ts:136](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/turn.ts#L136)

***

### 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)

***

### startLLM

▸ **startLLM**(`opts`): [`LLM`](./llm)

Start a child LLM span under this Turn.

#### Parameters

| Name   | Type                               |
| :----- | :--------------------------------- |
| `opts` | [`LLMInit`](../interfaces/llminit) |

#### Returns

[`LLM`](./llm)

#### Defined in

[src/genai/turn.ts:104](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/turn.ts#L104)

***

### startSubagent

▸ **startSubagent**(`opts`): [`SubAgent`](./subagent)

Start a child SubAgent span under this Turn.

#### Parameters

| Name   | Type                                         |
| :----- | :------------------------------------------- |
| `opts` | [`SubAgentInit`](../interfaces/subagentinit) |

#### Returns

[`SubAgent`](./subagent)

#### Defined in

[src/genai/turn.ts:122](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/turn.ts#L122)

***

### startTool

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

Start a child Tool span under this Turn.

#### Parameters

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

#### Returns

[`Tool`](./tool)

#### Defined in

[src/genai/turn.ts:113](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/turn.ts#L113)

***

### create

▸ **create**(`opts?`): [`Turn`](./turn)

#### Parameters

| Name   | Type                                                                     |
| :----- | :----------------------------------------------------------------------- |
| `opts` | [`TurnInit`](../interfaces/turninit) & \{ `conversationId?`: `string`  } |

#### Returns

[`Turn`](./turn)

#### Defined in

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