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

# LLM

> TypeScript SDK reference

Defined in: [src/genai/llm.ts:86](https://github.com/wandb/weave/blob/8c5f077eb11c42b84000726ff20504abc728d3fb/sdks/node/src/genai/llm.ts#L86)

An LLM call. Emits a `chat` span with `gen_ai.*` attributes.

Created by `weave.startLLM()` (or `turn.startLLM()`) and terminated with
`end()`. Only one LLM may be active in an async context at a time; nest
tool/subagent calls under it via `startTool` / `startSubagent`.

Populate `inputMessages` / `outputMessages` / `usage` / `reasoning` directly,
or via the helper functions (`output`, `think`, `attachMedia`, `record`).

All recorded data is flushed to the span at `end()`.

## Examples

```ts twoslash theme={null}
// @noErrors
const llm = weave.startLLM({model: 'gpt-4o-mini', providerName: 'openai'});

try {
  llm.inputMessages = [{role: 'user', content: prompt}];
  const resp = await openai.chat.completions.create({...});
  llm.output(resp.choices[0].message.content ?? '');
  llm.record({usage: {inputTokens: resp.usage?.prompt_tokens}});
} finally {
  llm.end();
}
```

```ts twoslash theme={null}
// @noErrors
const llm = weave.startLLM({
  model: 'gpt-4o-mini',
  providerName: 'openai',
  systemInstructions: ['You are a helpful weather bot.'],
  startTime: new Date('2026-05-29T10:00:00.000Z'),
});

try {
  // ... call the LLM, populate llm.outputMessages / usage ...
} finally {
  llm.end();
}
```

## Extends

* `SpanBase`

## Properties

### inputMessages

> **inputMessages**: [`Message`](./message)\[] = `[]`

Defined in: [src/genai/llm.ts:93](https://github.com/wandb/weave/blob/8c5f077eb11c42b84000726ff20504abc728d3fb/sdks/node/src/genai/llm.ts#L93)

Input messages sent to the model. Flushed to `gen_ai.input.messages` on
`end()`.

***

### model

> `readonly` **model**: `string`

Defined in: [src/genai/llm.ts:117](https://github.com/wandb/weave/blob/8c5f077eb11c42b84000726ff20504abc728d3fb/sdks/node/src/genai/llm.ts#L117)

***

### outputMessages

> **outputMessages**: [`Message`](./message)\[] = `[]`

Defined in: [src/genai/llm.ts:98](https://github.com/wandb/weave/blob/8c5f077eb11c42b84000726ff20504abc728d3fb/sdks/node/src/genai/llm.ts#L98)

Assistant messages returned by the model. Flushed to
`gen_ai.output.messages` on `end()`.

***

### providerName

> `readonly` **providerName**: `string`

Defined in: [src/genai/llm.ts:118](https://github.com/wandb/weave/blob/8c5f077eb11c42b84000726ff20504abc728d3fb/sdks/node/src/genai/llm.ts#L118)

***

### reasoning?

> `optional` **reasoning?**: [`Reasoning`](./reasoning)

Defined in: [src/genai/llm.ts:105](https://github.com/wandb/weave/blob/8c5f077eb11c42b84000726ff20504abc728d3fb/sdks/node/src/genai/llm.ts#L105)

Chain-of-thought content. Folded into the last assistant message as a
ReasoningPart at serialization time.

***

### usage

> **usage**: [`Usage`](./usage) = `{}`

Defined in: [src/genai/llm.ts:100](https://github.com/wandb/weave/blob/8c5f077eb11c42b84000726ff20504abc728d3fb/sdks/node/src/genai/llm.ts#L100)

Token counts and cache stats. Flushed to `gen_ai.usage.*` on `end()`.

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

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

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

`string`

##### attributes?

`Attributes`

##### startTime?

`TimeInput`

#### Returns

`this`

#### Example

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

#### Inherited from

`SpanBase.addEvent`

***

### attachMedia()

> **attachMedia**(`opts`): `this`

Defined in: [src/genai/llm.ts:183](https://github.com/wandb/weave/blob/8c5f077eb11c42b84000726ff20504abc728d3fb/sdks/node/src/genai/llm.ts#L183)

Stage a media attachment for the LLM call. Pick exactly one of
`content` (inline base64 bytes), `uri` (URI reference), or `fileId`
(pre-uploaded file id). The attachment is glued onto the last user
message in `inputMessages` on `end()`.

#### Parameters

##### opts

`AttachMediaOpts`

#### Returns

`this`

***

### attachMediaUrl()

> **attachMediaUrl**(`url`, `opts`): `this`

Defined in: [src/genai/llm.ts:192](https://github.com/wandb/weave/blob/8c5f077eb11c42b84000726ff20504abc728d3fb/sdks/node/src/genai/llm.ts#L192)

Convenience for `attachMedia({uri, modality})`.

#### Parameters

##### url

`string`

##### opts

###### modality

[`Modality`](../type-aliases/modality)

#### Returns

`this`

***

### end()

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

Defined in: [src/genai/llm.ts:274](https://github.com/wandb/weave/blob/8c5f077eb11c42b84000726ff20504abc728d3fb/sdks/node/src/genai/llm.ts#L274)

Flush accumulated state and close the span. Idempotent. Pass `error` to mark failed; pass `endTime` to backdate the close.

#### Parameters

##### opts?

`SpanEndOptions`

#### Returns

`void`

***

### output()

> **output**(`content`): `this`

Defined in: [src/genai/llm.ts:155](https://github.com/wandb/weave/blob/8c5f077eb11c42b84000726ff20504abc728d3fb/sdks/node/src/genai/llm.ts#L155)

Append an assistant message to the response.

#### Parameters

##### content

`string`

#### Returns

`this`

***

### record()

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

Defined in: [src/genai/llm.ts:203](https://github.com/wandb/weave/blob/8c5f077eb11c42b84000726ff20504abc728d3fb/sdks/node/src/genai/llm.ts#L203)

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

#### Parameters

##### opts

###### finishReasons?

`string`\[]

###### inputMessages?

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

###### mediaAttachments?

`AttachMediaOpts`\[]

###### outputMessages?

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

###### outputType?

`string`

###### reasoning?

[`Reasoning`](./reasoning)

###### responseId?

`string`

###### responseModel?

`string`

###### usage?

[`Usage`](./usage)

#### Returns

`this`

***

### setAttributes()

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

Defined in: [src/genai/spanBase.ts:63](https://github.com/wandb/weave/blob/8c5f077eb11c42b84000726ff20504abc728d3fb/sdks/node/src/genai/spanBase.ts#L63)

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

##### attributes

`Attributes`

#### Returns

`this`

#### Example

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

#### Inherited from

`SpanBase.setAttributes`

***

### startSubagent()

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

Defined in: [src/genai/llm.ts:261](https://github.com/wandb/weave/blob/8c5f077eb11c42b84000726ff20504abc728d3fb/sdks/node/src/genai/llm.ts#L261)

Start a child SubAgent span nested under this LLM.

#### Parameters

##### opts

[`SubAgentInit`](./subagentinit)

#### Returns

[`SubAgent`](./subagent)

***

### startTool()

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

Defined in: [src/genai/llm.ts:252](https://github.com/wandb/weave/blob/8c5f077eb11c42b84000726ff20504abc728d3fb/sdks/node/src/genai/llm.ts#L252)

Start a child Tool span nested under this LLM.

#### Parameters

##### opts

[`ToolInit`](./toolinit)

#### Returns

[`Tool`](./tool)

***

### think()

> **think**(`content`): `this`

Defined in: [src/genai/llm.ts:167](https://github.com/wandb/weave/blob/8c5f077eb11c42b84000726ff20504abc728d3fb/sdks/node/src/genai/llm.ts#L167)

Set or extend the model's reasoning/chain-of-thought content. Accumulates
into `this.reasoning.content`. Folded into the last assistant message as
a `ReasoningPart` at serialization time, matching the Python SDK's
on-the-wire shape.

#### Parameters

##### content

`string`

#### Returns

`this`
