Skip to main content
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
const  = weave.startLLM({: 'gpt-4o-mini', : 'openai'});
try {
  .inputMessages = [{: 'user', : prompt}];
  const  = await openai.chat.completions.create({...});
  .output(.choices[0].message.content ?? '');
  .record({: {: .usage?.prompt_tokens}});
} finally {
  .end();
}

Hierarchy

  • SpanBase LLM

Table of contents

Properties

Methods

Properties

inputMessages

inputMessages: Message[] = [] Input messages sent to the model. Flushed to gen_ai.input.messages on end().

Defined in

src/genai/llm.ts:79

model

Readonly model: string

Defined in

src/genai/llm.ts:97

outputMessages

outputMessages: Message[] = [] Assistant messages returned by the model. Flushed to gen_ai.output.messages on end().

Defined in

src/genai/llm.ts:84

providerName

Readonly providerName: string

Defined in

src/genai/llm.ts:98

reasoning

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

Defined in

src/genai/llm.ts:91

usage

usage: Usage = {} Token counts and cache stats. Flushed to gen_ai.usage.* on end().

Defined in

src/genai/llm.ts:86

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

NameType
namestring
attributes?Attributes
startTime?TimeInput

Returns

this Example
span.addEvent('context_compacted', {: 12});

Inherited from

SpanBase.addEvent

Defined in

src/genai/spanBase.ts:77

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

NameType
optsAttachMediaOpts

Returns

this

Defined in

src/genai/llm.ts:170

attachMediaUrl

attachMediaUrl(url, opts): this Convenience for attachMedia({uri, modality}).

Parameters

NameType
urlstring
optsObject
opts.modalityModality

Returns

this

Defined in

src/genai/llm.ts:188

end

end(opts?): void Flush accumulated state and close the span. Idempotent. Pass error to mark failed; pass endTime to backdate the close.

Parameters

NameType
opts?SpanEndOptions

Returns

void

Defined in

src/genai/llm.ts:245

output

output(content): this Append an assistant message to the response.

Parameters

NameType
contentstring

Returns

this

Defined in

src/genai/llm.ts:143

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

NameType
optsLLMRecordOpts

Returns

this

Defined in

src/genai/llm.ts:199

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

NameType
attributesAttributes

Returns

this Example
span.setAttributes({'weave.tag': 'prod', 'gen_ai.response.id': id});

Inherited from

SpanBase.setAttributes

Defined in

src/genai/spanBase.ts:63

startSubagent

startSubagent(opts): SubAgent Start a child SubAgent span nested under this LLM.

Parameters

NameType
optsSubAgentInit

Returns

SubAgent

Defined in

src/genai/llm.ts:232

startTool

startTool(opts): Tool Start a child Tool span nested under this LLM.

Parameters

NameType
optsToolInit

Returns

Tool

Defined in

src/genai/llm.ts:223

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

NameType
contentstring

Returns

this

Defined in

src/genai/llm.ts:155

create

create(opts): LLM

Parameters

NameType
optsLLMInit & ChildSpanContext

Returns

LLM

Defined in

src/genai/llm.ts:103