BedrockModel
Defined in: src/models/bedrock.ts:324
AWS Bedrock model provider implementation.
Implements the Model interface for AWS Bedrock using the Converse Stream API. Supports streaming responses, tool use, prompt caching, and comprehensive error handling.
Example
Section titled “Example”const provider = new BedrockModel({ modelConfig: { modelId: 'global.anthropic.claude-sonnet-4-6', maxTokens: 1024, temperature: 0.7 }, clientConfig: { region: 'us-west-2' }})
const messages: Message[] = [ { type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hello!' }] }]
for await (const event of provider.stream(messages)) { if (event.type === 'modelContentBlockDeltaEvent' && event.delta.type === 'textDelta') { process.stdout.write(event.delta.text) }}Extends
Section titled “Extends”Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new BedrockModel(options?): BedrockModel;Defined in: src/models/bedrock.ts:358
Creates a new BedrockModel instance.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
options? | BedrockModelOptions | Optional configuration for model and client |
Returns
Section titled “Returns”BedrockModel
Example
Section titled “Example”// Minimal configuration with defaultsconst provider = new BedrockModel({ region: 'us-west-2'})
// With model configurationconst provider = new BedrockModel({ region: 'us-west-2', modelId: 'global.anthropic.claude-sonnet-4-6', maxTokens: 2048, temperature: 0.8, cacheConfig: { strategy: 'auto' }})
// With client configurationconst provider = new BedrockModel({ region: 'us-east-1', clientConfig: { credentials: myCredentials }})Overrides
Section titled “Overrides”Accessors
Section titled “Accessors”modelId
Section titled “modelId”Get Signature
Section titled “Get Signature”get modelId(): string;Defined in: src/models/model.ts:205
The model ID from the current configuration, if configured.
Returns
Section titled “Returns”string
Inherited from
Section titled “Inherited from”stateful
Section titled “stateful”Get Signature
Section titled “Get Signature”get stateful(): boolean;Defined in: src/models/model.ts:221
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
Section titled “Returns”boolean
false by default
Inherited from
Section titled “Inherited from”Methods
Section titled “Methods”updateConfig()
Section titled “updateConfig()”updateConfig(modelConfig): void;Defined in: src/models/bedrock.ts:450
Updates the model configuration. Merges the provided configuration with existing settings.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
modelConfig | BedrockModelConfig | Configuration object with model-specific settings to update |
Returns
Section titled “Returns”void
Example
Section titled “Example”// Update temperature and maxTokensprovider.updateConfig({ temperature: 0.9, maxTokens: 2048})Overrides
Section titled “Overrides”getConfig()
Section titled “getConfig()”getConfig(): BedrockModelConfig;Defined in: src/models/bedrock.ts:465
Retrieves the current model configuration.
Returns
Section titled “Returns”The current configuration object
Example
Section titled “Example”const config = provider.getConfig()console.log(config.modelId)Overrides
Section titled “Overrides”countTokens()
Section titled “countTokens()”countTokens(messages, options?): Promise<number>;Defined in: src/models/bedrock.ts:479
Count tokens using Bedrock’s native CountTokens API.
Uses the same message format as the Converse API to get accurate token counts directly from the Bedrock service. Falls back to the base class heuristic on failure.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
messages | Message[] | Array of conversation messages to count tokens for |
options? | CountTokensOptions | Optional options containing system prompt and tool specs |
Returns
Section titled “Returns”Promise<number>
Total input token count
Overrides
Section titled “Overrides”stream()
Section titled “stream()”stream(messages, options?): AsyncIterable<ModelStreamEvent>;Defined in: src/models/bedrock.ts:535
Streams a conversation with the Bedrock model. Returns an async iterable that yields streaming events as they occur.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
messages | Message[] | Array of conversation messages |
options? | StreamOptions | Optional streaming configuration |
Returns
Section titled “Returns”AsyncIterable<ModelStreamEvent>
Async iterable of streaming events
Throws
Section titled “Throws”{ContextWindowOverflowError} When input exceeds the model’s context window
Throws
Section titled “Throws”{ModelThrottledError} When Bedrock service throttles requests
Example
Section titled “Example”const messages: Message[] = [ { type: 'message', role: $1, content: [{ type: 'textBlock', text: 'What is 2+2?' }] }]
const options: StreamOptions = { systemPrompt: 'You are a helpful math assistant.', toolSpecs: [calculatorTool]}
for await (const event of provider.stream(messages, options)) { if (event.type === 'modelContentBlockDeltaEvent') { console.log(event.delta) }}Overrides
Section titled “Overrides”streamAggregated()
Section titled “streamAggregated()”streamAggregated(messages, options?): AsyncGenerator< | ContentBlock| ModelStreamEvent, StreamAggregatedResult, undefined>;Defined in: src/models/model.ts:307
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
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
messages | Message[] | Array of conversation messages |
options? | StreamOptions | Optional streaming configuration |
Returns
Section titled “Returns”AsyncGenerator<
| ContentBlock
| ModelStreamEvent, StreamAggregatedResult, undefined>
Async generator yielding ModelStreamEvent | ContentBlock and returning a StreamAggregatedResult
Throws
Section titled “Throws”ModelError - Base class for all model-related errors
Throws
Section titled “Throws”ContextWindowOverflowError - When input exceeds the model’s context window
Throws
Section titled “Throws”ModelThrottledError - When the model provider throttles requests
Throws
Section titled “Throws”MaxTokensError - When the model reaches its maximum token limit