> ## Documentation Index
> Fetch the complete documentation index at: https://wb-21fd5541-update-reference-docs-59.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Turn

> TypeScript SDK reference

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();

try {
  const llm = turn.startLLM({model: 'gpt-4o', providerName: 'openai'});
  // ...
  llm.end();
} finally {
  turn.end();
}
```

`Example`

```ts twoslash theme={null}
// @noErrors
const turn = weave.startTurn({
  model: 'gpt-4o',
  agentName: 'research-bot',
  agentId: 'research-bot-prod',
  agentDescription: 'Looks up facts on Wikipedia.',
  agentVersion: '1.4.2',
  userMessage: 'What is the weather in Tokyo?',
  systemInstructions: ['You are a helpful weather bot.'],
  startTime: new Date('2026-05-29T10:00:00.000Z'),
});

try {
  const llm = turn.startLLM({model: 'gpt-4o', providerName: 'openai'});
  // ...
  llm.end();
} finally {
  turn.end();
}
```

## Hierarchy

* `SpanBase`

  ↳ `Turn`

## Table of contents

### Accessors

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

### Methods

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

## Accessors

### agentName

• `get` **agentName**(): `string`

#### Returns

`string`

#### Defined in

[src/genai/turn.ts:111](https://github.com/wandb/weave/blob/d5e11acb4bb292878563e3dca68e95312f03b6af/sdks/node/src/genai/turn.ts#L111)

***

### model

• `get` **model**(): `string`

#### Returns

`string`

#### Defined in

[src/genai/turn.ts:115](https://github.com/wandb/weave/blob/d5e11acb4bb292878563e3dca68e95312f03b6af/sdks/node/src/genai/turn.ts#L115)

## Methods

### addEvent

<Warning>
  **Deprecated.** Record this data via [setAttributes](./turn#setattributes) instead.
  OpenTelemetry is phasing out the Span Event API (`Span.addEvent`). This
  method still works and existing span-event data stays valid.
  See [https://opentelemetry.io/blog/2026/deprecating-span-events/](https://opentelemetry.io/blog/2026/deprecating-span-events/)

  `Example`

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

▸ **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`

#### Inherited from

SpanBase.addEvent

#### Defined in

[src/genai/spanBase.ts:82](https://github.com/wandb/weave/blob/d5e11acb4bb292878563e3dca68e95312f03b6af/sdks/node/src/genai/spanBase.ts#L82)

***

### end

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

Read current field values (to reflect mutations made via `record()`
since `start`) and close the 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:257](https://github.com/wandb/weave/blob/d5e11acb4bb292878563e3dca68e95312f03b6af/sdks/node/src/genai/turn.ts#L257)

***

### record

▸ **record**(`opts`): `this`

Bulk-set any subset of the mutable fields. Replaces (does not merge).
Useful for assigning everything at once after a provider call returns.

#### Parameters

| Name                       | Type                                  |
| :------------------------- | :------------------------------------ |
| `opts`                     | `Object`                              |
| `opts.agentDescription?`   | `string`                              |
| `opts.agentId?`            | `string`                              |
| `opts.agentName?`          | `string`                              |
| `opts.agentVersion?`       | `string`                              |
| `opts.messages?`           | [`Message`](../interfaces/message)\[] |
| `opts.model?`              | `string`                              |
| `opts.outputMessages?`     | [`Message`](../interfaces/message)\[] |
| `opts.systemInstructions?` | `string`\[]                           |

#### Returns

`this`

#### Defined in

[src/genai/turn.ts:213](https://github.com/wandb/weave/blob/d5e11acb4bb292878563e3dca68e95312f03b6af/sdks/node/src/genai/turn.ts#L213)

***

### 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:205](https://github.com/wandb/weave/blob/d5e11acb4bb292878563e3dca68e95312f03b6af/sdks/node/src/genai/turn.ts#L205)

***

### 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/d5e11acb4bb292878563e3dca68e95312f03b6af/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:170](https://github.com/wandb/weave/blob/d5e11acb4bb292878563e3dca68e95312f03b6af/sdks/node/src/genai/turn.ts#L170)

***

### 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:190](https://github.com/wandb/weave/blob/d5e11acb4bb292878563e3dca68e95312f03b6af/sdks/node/src/genai/turn.ts#L190)

***

### 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:180](https://github.com/wandb/weave/blob/d5e11acb4bb292878563e3dca68e95312f03b6af/sdks/node/src/genai/turn.ts#L180)

***

### create

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

#### Parameters

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

#### Returns

[`Turn`](./turn)

#### Defined in

[src/genai/turn.ts:133](https://github.com/wandb/weave/blob/d5e11acb4bb292878563e3dca68e95312f03b6af/sdks/node/src/genai/turn.ts#L133)
