Skip to content

Model

Defined in: src/models/model.ts:177

Base abstract class for model providers. Defines the contract that all model provider implementations must follow.

Model providers handle communication with LLM APIs and implement streaming responses using async iterables.

Type ParameterDefault typeDescription
T extends BaseModelConfigBaseModelConfigModel configuration type extending BaseModelConfig
new Model<T>(): Model<T>;

Model<T>

get modelId(): string;

Defined in: src/models/model.ts:196

The model ID from the current configuration, if configured.

string

abstract updateConfig(modelConfig): void;

Defined in: src/models/model.ts:184

Updates the model configuration. Merges the provided configuration with existing settings.

ParameterTypeDescription
modelConfigTConfiguration object with model-specific settings to update

void


abstract getConfig(): T;

Defined in: src/models/model.ts:191

Retrieves the current model configuration.

T

The current configuration object


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

Defined in: src/models/model.ts:208

Streams a conversation with the model. Returns an async iterable that yields streaming events as they occur.

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

AsyncIterable<ModelStreamEvent>

Async iterable of streaming events


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

Defined in: src/models/model.ts:224

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.

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

Promise<number>

Total input token count


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

Defined in: src/models/model.ts:282

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.

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

AsyncGenerator< | ContentBlock | ModelStreamEvent, StreamAggregatedResult, undefined>

Async generator yielding ModelStreamEvent | ContentBlock and returning a StreamAggregatedResult

ModelError - Base class for all model-related errors

ContextWindowOverflowError - When input exceeds the model’s context window

ModelThrottledError - When the model provider throttles requests

MaxTokensError - When the model reaches its maximum token limit