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();
}
Example
const  = weave.startLLM({
  : 'gpt-4o-mini',
  : 'openai',
  : ['You are a helpful weather bot.'],
  : new ('2026-05-29T10:00:00.000Z'),
});

try {
  // ... call the LLM, populate llm.outputMessages / usage ...
} 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:93

model

Readonly model: string

Defined in

src/genai/llm.ts:117

outputMessages

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

Defined in

src/genai/llm.ts:98

providerName

Readonly providerName: string

Defined in

src/genai/llm.ts:118

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:105

usage

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

Defined in

src/genai/llm.ts:100

Methods

addEvent

Deprecated. Record this data via 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/Example
span.addEvent('context_compacted', {: 12});
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

Inherited from

SpanBase.addEvent

Defined in

src/genai/spanBase.ts:82

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

NameType
optsAttachMediaOpts

Returns

this

Defined in

src/genai/llm.ts:183

attachMediaUrl

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

Parameters

NameType
urlstring
optsObject
opts.modalityModality

Returns

this

Defined in

src/genai/llm.ts:192

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:274

output

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

Parameters

NameType
contentstring

Returns

this

Defined in

src/genai/llm.ts:155

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
optsObject
opts.finishReasons?string[]
opts.inputMessages?Message[]
opts.mediaAttachments?AttachMediaOpts[]
opts.outputMessages?Message[]
opts.outputType?string
opts.reasoning?Reasoning
opts.responseId?string
opts.responseModel?string
opts.usage?Usage

Returns

this

Defined in

src/genai/llm.ts:203

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:261

startTool

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

Parameters

NameType
optsToolInit

Returns

Tool

Defined in

src/genai/llm.ts:252

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:167

create

create(opts): LLM

Parameters

NameType
optsLLMInit & ChildSpanContext

Returns

LLM

Defined in

src/genai/llm.ts:124