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

## 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:79](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/llm.ts#L79)

***

### model

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

#### Defined in

[src/genai/llm.ts:97](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/llm.ts#L97)

***

### 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:84](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/llm.ts#L84)

***

### providerName

• `Readonly` **providerName**: `string`

#### Defined in

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

***

### 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:91](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/llm.ts#L91)

***

### usage

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

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

#### Defined in

[src/genai/llm.ts:86](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/llm.ts#L86)

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

***

### attachMedia

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

Attach a media part to the last input message. Pick exactly one of
`content` (inline base64 bytes), `uri` (URI reference), or `fileId`
(pre-uploaded file id).

#### Parameters

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

#### Returns

`this`

#### Defined in

[src/genai/llm.ts:170](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/llm.ts#L170)

***

### 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:188](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/llm.ts#L188)

***

### 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:245](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/llm.ts#L245)

***

### output

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

Append an assistant message to the response.

#### Parameters

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

#### Returns

`this`

#### Defined in

[src/genai/llm.ts:143](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/llm.ts#L143)

***

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

#### Returns

`this`

#### Defined in

[src/genai/llm.ts:199](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/llm.ts#L199)

***

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

***

### 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:232](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/llm.ts#L232)

***

### 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:223](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/llm.ts#L223)

***

### 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:155](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/llm.ts#L155)

***

### create

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

#### Parameters

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

#### Returns

[`LLM`](./llm)

#### Defined in

[src/genai/llm.ts:103](https://github.com/wandb/weave/blob/9591aba8a5e77309ee0858e5ba94d31c1bd7e404/sdks/node/src/genai/llm.ts#L103)
