> ## Documentation Index
> Fetch the complete documentation index at: https://wb-21fd5541-drtangible-wip-test-inline-tag.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.

Defined in: [src/genai/turn.ts:98](https://github.com/wandb/weave/blob/7c9efdc9fe05ffcaf2430dd652ef9bc5b3ffdfaa/sdks/node/src/genai/turn.ts#L98)

## Examples

```ts twoslash theme={null}
// @noErrors
const turn = weave.startTurn();

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

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

## Extends

* `SpanBase`

## Accessors

### agentName

#### Get Signature

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

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

##### Returns

`string`

***

### model

#### Get Signature

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

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

##### Returns

`string`

## Methods

### ~~addEvent()~~

<Warning>
  **Deprecated.** Record this data via [setAttributes](#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/)
</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`.

Defined in: [src/genai/spanBase.ts:84](https://github.com/wandb/weave/blob/7c9efdc9fe05ffcaf2430dd652ef9bc5b3ffdfaa/sdks/node/src/genai/spanBase.ts#L84)

#### Parameters

##### name

`string`

##### attributes?

`Attributes`

##### startTime?

`TimeInput`

#### Returns

`this`

#### Example

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

#### Inherited from

`SpanBase.addEvent`

***

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

Defined in: [src/genai/turn.ts:257](https://github.com/wandb/weave/blob/7c9efdc9fe05ffcaf2430dd652ef9bc5b3ffdfaa/sdks/node/src/genai/turn.ts#L257)

#### Parameters

##### opts?

###### endTime?

`TimeInput`

###### error?

`Error`

#### Returns

`void`

***

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

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

#### Parameters

##### opts

###### agentDescription?

`string`

###### agentId?

`string`

###### agentName?

`string`

###### agentVersion?

`string`

###### messages?

[`Message`](./message)\[]

###### model?

`string`

###### outputMessages?

[`Message`](./message)\[]

###### systemInstructions?

`string`\[]

#### Returns

`this`

***

### ~~setAttribute()~~

<Warning>
  **Deprecated.** Use [setAttributes](#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`

Defined in: [src/genai/turn.ts:205](https://github.com/wandb/weave/blob/7c9efdc9fe05ffcaf2430dd652ef9bc5b3ffdfaa/sdks/node/src/genai/turn.ts#L205)

#### Parameters

##### key

`string`

##### value

`AttributeValue`

#### Returns

`this`

***

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

Defined in: [src/genai/spanBase.ts:65](https://github.com/wandb/weave/blob/7c9efdc9fe05ffcaf2430dd652ef9bc5b3ffdfaa/sdks/node/src/genai/spanBase.ts#L65)

#### Parameters

##### attributes

`Attributes`

#### Returns

`this`

#### Example

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

#### Inherited from

`SpanBase.setAttributes`

***

### startLLM()

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

Start a child LLM span under this Turn.

Defined in: [src/genai/turn.ts:170](https://github.com/wandb/weave/blob/7c9efdc9fe05ffcaf2430dd652ef9bc5b3ffdfaa/sdks/node/src/genai/turn.ts#L170)

#### Parameters

##### opts

[`LLMInit`](./llminit)

#### Returns

[`LLM`](./llm)

***

### startSubagent()

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

Start a child SubAgent span under this Turn.

Defined in: [src/genai/turn.ts:190](https://github.com/wandb/weave/blob/7c9efdc9fe05ffcaf2430dd652ef9bc5b3ffdfaa/sdks/node/src/genai/turn.ts#L190)

#### Parameters

##### opts

[`SubAgentInit`](./subagentinit)

#### Returns

[`SubAgent`](./subagent)

***

### startTool()

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

Start a child Tool span under this Turn.

Defined in: [src/genai/turn.ts:180](https://github.com/wandb/weave/blob/7c9efdc9fe05ffcaf2430dd652ef9bc5b3ffdfaa/sdks/node/src/genai/turn.ts#L180)

#### Parameters

##### opts

[`ToolInit`](./toolinit)

#### Returns

[`Tool`](./tool)
