Skip to main content

Class: TemporalModel

strands.TemporalModel

A Strands Model whose Model.stream runs as a Temporal activity. Constructing a TemporalModel does no I/O, so instances are safe to create at module load time.

The modelName selects which factory the plugin will invoke worker-side; it must match a key in StrandsPlugin's models map. A name not present in models raises an error inside the model activity at first call.

Hierarchy

  • Model<BaseModelConfig>

    TemporalModel

Constructors

constructor

new TemporalModel(options?): TemporalModel

Parameters

NameType
options?TemporalModelOptions

Returns

TemporalModel

Overrides

Model&lt;BaseModelConfig&gt;.constructor

Accessors

modelId

get modelId(): undefined | string

The model ID from the current configuration, if configured.

Returns

undefined | string

Inherited from

Model.modelId


stateful

get stateful(): boolean

Whether this model manages conversation state server-side.

When true, the server tracks conversation context across turns, so the SDK sends only the latest message instead of the full history. After each invocation, the agent's local message history is cleared automatically.

Model providers that support server-side state management should override this to return true.

Returns

boolean

false by default

Inherited from

Model.stateful

Methods

countTokens

countTokens(messages, options?): Promise<number>

Count tokens for the given input before sending to the model.

Used for proactive context management (e.g., triggering compression at a threshold). The base implementation uses a character-based heuristic (chars/4 for text, chars/2 for JSON).

Subclasses should override this method to use native token counting APIs (e.g., Bedrock CountTokens, Anthropic countTokens, Gemini countTokens) for improved accuracy, falling back to super.countTokens() on API failure.

Parameters

NameTypeDescription
messagesMessage[]Array of conversation messages to count tokens for
options?CountTokensOptionsOptional options containing system prompt and tool specs

Returns

Promise<number>

Total input token count

Inherited from

Model.countTokens


getConfig

getConfig(): BaseModelConfig

Returns

BaseModelConfig

Overrides

Model.getConfig


stream

stream(messages, options?): AsyncIterable<ModelStreamEvent>

Parameters

NameType
messagesMessage[]
options?StreamOptions

Returns

AsyncIterable<ModelStreamEvent>

Overrides

Model.stream


streamAggregated

streamAggregated(messages, options?): AsyncGenerator<ModelStreamEvent | ContentBlock, StreamAggregatedResult, undefined>

Streams a conversation with aggregated content blocks and messages. Returns an async generator that yields streaming events and content blocks, and returns the final message with stop reason and optional metadata.

This method enhances the basic stream() by collecting streaming events into complete ContentBlock and Message objects, which are needed by the agentic loop for tool execution and conversation management.

The method yields:

  • ModelStreamEvent - Original streaming events (passed through)
  • ContentBlock - Complete content block (emitted when block completes)

The method returns:

  • StreamAggregatedResult containing the complete message, stop reason, and optional metadata

All exceptions thrown from this method are wrapped in ModelError to provide a consistent error type for model-related errors. Specific error subtypes like ContextWindowOverflowError, ModelThrottledError, and MaxTokensError are preserved.

Parameters

NameTypeDescription
messagesMessage[]Array of conversation messages
options?StreamOptionsOptional streaming configuration

Returns

AsyncGenerator<ModelStreamEvent | ContentBlock, StreamAggregatedResult, undefined>

Async generator yielding ModelStreamEvent | ContentBlock and returning a StreamAggregatedResult

Throws

ModelError - Base class for all model-related errors

Throws

ContextWindowOverflowError - When input exceeds the model's context window

Throws

ModelThrottledError - When the model provider throttles requests

Throws

MaxTokensError - When the model reaches its maximum token limit

Inherited from

Model.streamAggregated


updateConfig

updateConfig(_config): void

Parameters

NameType
_configBaseModelConfig

Returns

void

Overrides

Model.updateConfig