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

> TypeScript SDK reference

[weave](../) / LLM

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

`Example`

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

`Example`

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

## Hierarchy

* `SpanBase`

  ↳ `LLM`

## Table of contents

### Properties

* [inputMessages](./llm#inputmessages)
* [model](./llm#model)
* [outputMessages](./llm#outputmessages)
* [providerName](./llm#providername)
* [reasoning](./llm#reasoning)
* [usage](./llm#usage)

### Methods

* [addEvent](./llm#addevent)
* [attachMedia](./llm#attachmedia)
* [attachMediaUrl](./llm#attachmediaurl)
* [end](./llm#end)
* [output](./llm#output)
* [record](./llm#record)
* [setAttributes](./llm#setattributes)
* [startSubagent](./llm#startsubagent)
* [startTool](./llm#starttool)
* [think](./llm#think)
* [create](./llm#create)

## Properties

### inputMessages

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

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

#### Defined in

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

***

### 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`](../interfaces/message)\[] = `[]`

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

#### Defined in

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

***

### 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`](../interfaces/reasoning)

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

#### Defined in

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

***

### usage

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

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

#### Defined in

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

## Methods

### addEvent

<Warning>
  **Deprecated.** Record this data via [setAttributes](./llm#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/8c5f077eb11c42b84000726ff20504abc728d3fb/sdks/node/src/genai/spanBase.ts#L82)

***

### attachMedia

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

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

| Name   | Type              |
| :----- | :---------------- |
| `opts` | `AttachMediaOpts` |

#### Returns

`this`

#### Defined in

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

***

### attachMediaUrl

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

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

#### Parameters

| Name            | Type                       |
| :-------------- | :------------------------- |
| `url`           | `string`                   |
| `opts`          | `Object`                   |
| `opts.modality` | [`Modality`](../#modality) |

#### Returns

`this`

#### Defined in

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

***

### end

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

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

#### Parameters

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

#### Returns

`void`

#### Defined in

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

***

### output

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

Append an assistant message to the response.

#### Parameters

| Name      | Type     |
| :-------- | :------- |
| `content` | `string` |

#### Returns

`this`

#### Defined in

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

***

### 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.finishReasons?`    | `string`\[]                            |
| `opts.inputMessages?`    | [`Message`](../interfaces/message)\[]  |
| `opts.mediaAttachments?` | `AttachMediaOpts`\[]                   |
| `opts.outputMessages?`   | [`Message`](../interfaces/message)\[]  |
| `opts.outputType?`       | `string`                               |
| `opts.reasoning?`        | [`Reasoning`](../interfaces/reasoning) |
| `opts.responseId?`       | `string`                               |
| `opts.responseModel?`    | `string`                               |
| `opts.usage?`            | [`Usage`](../interfaces/usage)         |

#### Returns

`this`

#### Defined in

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

***

### 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/8c5f077eb11c42b84000726ff20504abc728d3fb/sdks/node/src/genai/spanBase.ts#L63)

***

### startSubagent

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

Start a child SubAgent span nested under this LLM.

#### Parameters

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

#### Returns

[`SubAgent`](./subagent)

#### Defined in

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

***

### startTool

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

Start a child Tool span nested under this LLM.

#### Parameters

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

#### Returns

[`Tool`](./tool)

#### Defined in

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

***

### think

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

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

| Name      | Type     |
| :-------- | :------- |
| `content` | `string` |

#### Returns

`this`

#### Defined in

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

***

### create

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

#### Parameters

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

#### Returns

[`LLM`](./llm)

#### Defined in

[src/genai/llm.ts:124](https://github.com/wandb/weave/blob/8c5f077eb11c42b84000726ff20504abc728d3fb/sdks/node/src/genai/llm.ts#L124)
