Amazon Bedrock (Generative AI)
The Bedrock Converse API provides a unified interface for calling Claude, Nova, Llama, and other foundation models with streaming, multimodal input, tool use, system prompts, and inference parameters.
Busca en todas las páginas de la documentación
The Bedrock Converse API provides a unified interface for calling Claude, Nova, Llama, and other foundation models with streaming, multimodal input, tool use, system prompts, and inference parameters.
One unified API call across Claude, Nova, Llama, and Titan model families.
# --- Python (boto3) ---
import boto3
br = boto3.client("bedrock-runtime")
r = br.converse(modelId="anthropic.claude-3-5-sonnet-20241022-v2:0",
messages=[{"role": "user", "content": [{"text": "Hello"}]}])
print(r["output"]["message"]["content"][0]["text"])// --- TypeScript (AWS SDK v3) ---
import { BedrockRuntimeClient, ConverseCommand } from "@aws-sdk/client-bedrock-runtime";
const br = new BedrockRuntimeClient({});
const r = await br.send(new ConverseCommand({ modelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
messages: [{ role: "user", content: [{ text: "Hello" }] }] }));
console.log(r.output.message.content[0].text);Receive tokens incrementally with the Converse API using the converse_stream method.
# --- Python (boto3) ---
import boto3
br = boto3.client("bedrock-runtime")
stream = br.converse_stream(modelId="anthropic.claude-3-5-sonnet-20241022-v2:0",
messages=[{"role": "user", "content": [{"text": "Count to 5"}]}])
for event in stream["stream"]:
if "contentBlockDelta" in event:
print(event["contentBlockDelta"]["delta"]["text"], end="")// --- TypeScript (AWS SDK v3) ---
import { BedrockRuntimeClient, ConverseStreamCommand } from "@aws-sdk/client-bedrock-runtime";
const br = new BedrockRuntimeClient({});
const r = await br.send(new ConverseStreamCommand({ modelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
messages: [{ role: "user", content: [{ text: "Count to 5" }] }] }));
for await (const event of r.stream) {
if (event.contentBlockDelta) process.stdout.write(event.contentBlockDelta.delta.text);
}Configure the system message and inference parameters like temperature and max tokens.
# --- Python (boto3) ---
import boto3
br = boto3.client("bedrock-runtime")
r = br.converse(modelId="anthropic.claude-3-5-sonnet-20241022-v2:0",
system=[{"text": "You are a helpful assistant."}],
messages=[{"role": "user", "content": [{"text": "Hi"}]}],
inferenceConfig={"maxTokens": 200, "temperature": 0.7})// --- TypeScript (AWS SDK v3) ---
import { BedrockRuntimeClient, ConverseCommand } from "@aws-sdk/client-bedrock-runtime";
const br = new BedrockRuntimeClient({});
const r = await br.send(new ConverseCommand({ modelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
system: [{ text: "You are a helpful assistant." }],
messages: [{ role: "user", content: [{ text: "Hi" }] }],
inferenceConfig: { maxTokens: 200, temperature: 0.7 } }));Build conversation history by appending assistant and user messages to maintain context.
# --- Python (boto3) ---
import boto3
br = boto3.client("bedrock-runtime")
messages = [
{"role": "user", "content": [{"text": "What is 2+2?"}]},
{"role": "assistant", "content": [{"text": "The answer is 4."}]},
{"role": "user", "content": [{"text": "And 3+3?"}]}
]
r = br.converse(modelId="anthropic.claude-3-5-sonnet-20241022-v2:0", messages=messages)// --- TypeScript (AWS SDK v3) ---
import { BedrockRuntimeClient, ConverseCommand } from "@aws-sdk/client-bedrock-runtime";
const br = new BedrockRuntimeClient({});
const messages = [
{ role: "user", content: [{ text: "What is 2+2?" }] },
{ role: "assistant", content: [{ text: "The answer is 4." }] },
{ role: "user", content: [{ text: "And 3+3?" }] }
];
await br.send(new ConverseCommand({ modelId: "anthropic.claude-3-5-sonnet-20241022-v2:0", messages }));Include images as base64-encoded content in the message for vision tasks.
# --- Python (boto3) ---
import boto3, base64
br = boto3.client("bedrock-runtime")
with open("image.jpg", "rb") as f:
img_bytes = f.read()
r = br.converse(modelId="anthropic.claude-3-5-sonnet-20241022-v2:0",
messages=[{"role": "user", "content": [{"image": {"format": "jpeg", "source": {"bytes": img_bytes}}}, {"text": "What is this?"}]}])// --- TypeScript (AWS SDK v3) ---
import { BedrockRuntimeClient, ConverseCommand } from "@aws-sdk/client-bedrock-runtime";
import { readFileSync } from "fs";
const br = new BedrockRuntimeClient({});
const imgBytes = readFileSync("image.jpg");
const r = await br.send(new ConverseCommand({ modelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
messages: [{ role: "user", content: [{ image: { format: "jpeg", source: { bytes: imgBytes } } }, { text: "What is this?" }] }] }));Define tools and let the model decide when to call them, then return results for agentic workflows.
# --- Python (boto3) ---
import boto3
br = boto3.client("bedrock-runtime")
tools = [{"toolSpec": {"name": "get_weather", "description": "Get weather for a city",
"inputSchema": {"json": {"type": "object", "properties": {"city": {"type": "string"}}}}}}]
r = br.converse(modelId="anthropic.claude-3-5-sonnet-20241022-v2:0",
messages=[{"role": "user", "content": [{"text": "What is the weather in NYC?"}]}], toolConfig={"tools": tools})// --- TypeScript (AWS SDK v3) ---
import { BedrockRuntimeClient, ConverseCommand } from "@aws-sdk/client-bedrock-runtime";
const br = new BedrockRuntimeClient({});
const tools = [{ toolSpec: { name: "get_weather", description: "Get weather for a city",
inputSchema: { json: { type: "object", properties: { city: { type: "string" } } } } } }];
const r = await br.send(new ConverseCommand({ modelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
messages: [{ role: "user", content: [{ text: "What is the weather in NYC?" }] }], toolConfig: { tools } }));Call a model with its native payload format for direct protocol access.
# --- Python (boto3) ---
import boto3, json
br = boto3.client("bedrock-runtime")
payload = {"anthropic_version": "bedrock-2023-06-01", "max_tokens": 1024,
"messages": [{"role": "user", "content": "Hello"}]}
r = br.invoke_model(modelId="anthropic.claude-3-5-sonnet-20241022-v2:0", body=json.dumps(payload))
print(json.loads(r["body"].read())["content"][0]["text"])// --- TypeScript (AWS SDK v3) ---
import { BedrockRuntimeClient, InvokeModelCommand } from "@aws-sdk/client-bedrock-runtime";
const br = new BedrockRuntimeClient({});
const payload = { anthropic_version: "bedrock-2023-06-01", max_tokens: 1024,
messages: [{ role: "user", content: "Hello" }] };
const r = await br.send(new InvokeModelCommand({ modelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
body: JSON.stringify(payload) }));
const text = JSON.parse(new TextDecoder().decode(r.body)).content[0].text;Stream the model response using the lower-level InvokeModel API for direct protocol streaming.
# --- Python (boto3) ---
import boto3, json
br = boto3.client("bedrock-runtime")
payload = {"anthropic_version": "bedrock-2023-06-01", "max_tokens": 1024,
"messages": [{"role": "user", "content": "Count to 3"}]}
r = br.invoke_model_with_response_stream(modelId="anthropic.claude-3-5-sonnet-20241022-v2:0",
body=json.dumps(payload))
for event in r["body"]:
chunk = json.loads(event["chunk"]["bytes"])
if "delta" in chunk:
print(chunk["delta"]["text"], end="")// --- TypeScript (AWS SDK v3) ---
import { BedrockRuntimeClient, InvokeModelWithResponseStreamCommand } from "@aws-sdk/client-bedrock-runtime";
const br = new BedrockRuntimeClient({});
const payload = { anthropic_version: "bedrock-2023-06-01", max_tokens: 1024,
messages: [{ role: "user", content: "Count to 3" }] };
const r = await br.send(new InvokeModelWithResponseStreamCommand({ modelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
body: JSON.stringify(payload) }));
for await (const event of r.body) {
const chunk = JSON.parse(new TextDecoder().decode(event.chunk.bytes));
if (chunk.delta) process.stdout.write(chunk.delta.text);
}Convert text to embeddings using the Titan Embedding model for semantic search and RAG.
# --- Python (boto3) ---
import boto3, json
br = boto3.client("bedrock-runtime")
payload = {"inputText": "Hello world", "dimensions": 1536}
r = br.invoke_model(modelId="amazon.titan-embed-text-v2:0", body=json.dumps(payload))
embedding = json.loads(r["body"].read())["embedding"]
print(f"Embedding length: {len(embedding)}")// --- TypeScript (AWS SDK v3) ---
import { BedrockRuntimeClient, InvokeModelCommand } from "@aws-sdk/client-bedrock-runtime";
const br = new BedrockRuntimeClient({});
const payload = { inputText: "Hello world", dimensions: 1536 };
const r = await br.send(new InvokeModelCommand({ modelId: "amazon.titan-embed-text-v2:0",
body: JSON.stringify(payload) }));
const output = JSON.parse(new TextDecoder().decode(r.body));
console.log(`Embedding length: ${output.embedding.length}`);Retrieve a list of available foundation models in Bedrock with metadata.
# --- Python (boto3) ---
import boto3
bedrock = boto3.client("bedrock")
r = bedrock.list_foundation_models()
for model in r["modelSummaries"]:
print(f"{model['modelId']}: {model['modelName']}")// --- TypeScript (AWS SDK v3) ---
import { BedrockClient, ListFoundationModelsCommand } from "@aws-sdk/client-bedrock";
const bedrock = new BedrockClient({});
const r = await bedrock.send(new ListFoundationModelsCommand({}));
r.modelSummaries?.forEach(m => console.log(`${m.modelId}: ${m.modelName}`));Query a Bedrock Knowledge Base to retrieve relevant documents for RAG workflows.
# --- Python (boto3) ---
import boto3
ragclient = boto3.client("bedrock-agent-runtime")
r = ragclient.retrieve(knowledgeBaseId="abc123", retrievalConfiguration={"vectorSearchConfiguration": {"numberOfResults": 5}},
text="What is machine learning?")
for result in r["retrievalResults"]:
print(result["content"]["text"])// --- TypeScript (AWS SDK v3) ---
import { BedrockAgentRuntimeClient, RetrieveCommand } from "@aws-sdk/client-bedrock-agent-runtime";
const ragclient = new BedrockAgentRuntimeClient({});
const r = await ragclient.send(new RetrieveCommand({ knowledgeBaseId: "abc123",
retrievalConfiguration: { vectorSearchConfiguration: { numberOfResults: 5 } },
text: "What is machine learning?" }));
r.retrievalResults?.forEach(result => console.log(result.content.text));Automatically retrieve documents and generate a response in one call using Managed RAG.
# --- Python (boto3) ---
import boto3
ragclient = boto3.client("bedrock-agent-runtime")
r = ragclient.retrieve_and_generate(input={"text": "Explain quantum computing"},
retrieveAndGenerateConfiguration={"type": "KNOWLEDGE_BASE",
"knowledgeBaseConfiguration": {"knowledgeBaseId": "abc123"}})
print(r["output"]["text"])// --- TypeScript (AWS SDK v3) ---
import { BedrockAgentRuntimeClient, RetrieveAndGenerateCommand } from "@aws-sdk/client-bedrock-agent-runtime";
const ragclient = new BedrockAgentRuntimeClient({});
const r = await ragclient.send(new RetrieveAndGenerateCommand({ input: { text: "Explain quantum computing" },
retrieveAndGenerateConfiguration: { type: "KNOWLEDGE_BASE",
knowledgeBaseConfiguration: { knowledgeBaseId: "abc123" } } }));
console.log(r.output.text);Store vectors to S3 Vector Store (verify exact method names in current docs - new 2026 API).
# --- Python (boto3) ---
import boto3
s3v = boto3.client("s3vectors")
r = s3v.put_vectors(vectorStoreId="vs-abc123", vectors=[
{"id": "doc1", "embedding": [0.1, 0.2, 0.3], "metadata": {"title": "Doc 1"}}
])
print(f"Inserted: {len(r.get('ids', []))}")// --- TypeScript (AWS SDK v3) ---
import { S3VectorsClient, PutVectorsCommand } from "@aws-sdk/client-s3vectors";
const s3v = new S3VectorsClient({});
const r = await s3v.send(new PutVectorsCommand({ vectorStoreId: "vs-abc123",
vectors: [{ id: "doc1", embedding: [0.1, 0.2, 0.3], metadata: { title: "Doc 1" } }] }));
console.log(`Inserted: ${r.ids?.length}`);Search for vectors similar to a query embedding in S3 Vector Store (verify docs - new 2026 API).
# --- Python (boto3) ---
import boto3
s3v = boto3.client("s3vectors")
r = s3v.query_vectors(vectorStoreId="vs-abc123", queryEmbedding=[0.1, 0.2, 0.3],
numberOfResults=10)
for match in r.get("matches", []):
print(f"ID: {match['id']}, Score: {match['score']}")// --- TypeScript (AWS SDK v3) ---
import { S3VectorsClient, QueryVectorsCommand } from "@aws-sdk/client-s3vectors";
const s3v = new S3VectorsClient({});
const r = await s3v.send(new QueryVectorsCommand({ vectorStoreId: "vs-abc123",
queryEmbedding: [0.1, 0.2, 0.3], numberOfResults: 10 }));
r.matches?.forEach(m => console.log(`ID: ${m.id}, Score: ${m.score}`));Catch and handle token limit and throttling errors gracefully with retry logic.
# --- Python (boto3) ---
import boto3
from botocore.exceptions import ClientError
br = boto3.client("bedrock-runtime")
try:
r = br.converse(modelId="anthropic.claude-3-5-sonnet-20241022-v2:0",
messages=[{"role": "user", "content": [{"text": "Hi"}]}])
except ClientError as e:
if e.response["Error"]["Code"] == "ValidationException":
print("Token limit exceeded")
elif e.response["Error"]["Code"] == "ThrottlingException":
print("Rate limited - retry with backoff")// --- TypeScript (AWS SDK v3) ---
import { BedrockRuntimeClient, ConverseCommand } from "@aws-sdk/client-bedrock-runtime";
const br = new BedrockRuntimeClient({});
try {
await br.send(new ConverseCommand({ modelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
messages: [{ role: "user", content: [{ text: "Hi" }] }] }));
} catch (err) {
if ((err as any).Code === "ValidationException") console.log("Token limit exceeded");
else if ((err as any).Code === "ThrottlingException") console.log("Rate limited");
}Stack versions: This page was written for boto3 1.43.x (Python 3.10+) and the AWS SDK for JavaScript v3 (Node.js 18+). Bedrock AgentCore/S3 Vectors are a fast-moving 2026 surface - verify exact method names against current docs.
Revisado por Chris St. John·Última actualización: 23 jul 2026