Small Python library for invoking Claude 4.x models on AWS Bedrock with manual retries, prompt caching helpers, and JSON parsing.
Project description
AWS Bedrock Invoke Python
AWS Bedrock Invoke Python is a small library for invoking Claude 4.x models on AWS Bedrock with one consistent request path.
It focuses only on the transport layer:
- boto3 client creation
- optional client injection
- Bedrock model ID / inference-profile resolution
- Bedrock prompt-cache helpers
- manual retry with env-driven configuration
- response text extraction
- fenced JSON cleanup and parsing
It intentionally does not own business rules such as batching strategies, workflow orchestration, application result caching, or prompt authoring.
Installation
pip install aws-bedrock-invoke
Scope
This library is intentionally narrow:
- Claude 4.x on AWS Bedrock only
- manual retry managed by the library
- Bedrock prompt-cache support
- text and JSON invoke helpers
This library intentionally does not include:
- business-specific batching/windowing
- application result caching
- Slack notifications or persistence
- prompt engineering or workflow orchestration
Quick Start
invoke_json()
from aws_bedrock_invoke import BedrockInvoker, json_text_block
invoker = BedrockInvoker.from_env()
result = invoker.invoke_json(
model="us.anthropic.claude-sonnet-4-5-20250929-v1:0",
profile_arn_fmt="arn:aws:bedrock:{region}:354173731728:inference-profile/{model}",
system_prompt="Reply with strict JSON.",
user_content=[
json_text_block({"question": "hello"}),
],
)
print(result.parsed_json)
print(result.response_text)
invoke_text()
from aws_bedrock_invoke import BedrockInvoker, text_block
invoker = BedrockInvoker.from_env()
result = invoker.invoke_text(
model="us.anthropic.claude-sonnet-4-5-20250929-v1:0",
profile_arn_fmt="arn:aws:bedrock:{region}:354173731728:inference-profile/{model}",
system_prompt="Answer briefly.",
user_content=[
text_block("What is a lien waiver?"),
],
)
print(result.response_text)
Client Injection
Client injection is optional.
If you do not provide a client, BedrockInvoker builds one from
BedrockClientConfig.
If you already have a prepared boto Bedrock client, you can inject it directly. This is especially useful for adapters and tests.
import boto3
from aws_bedrock_invoke import BedrockClientConfig, BedrockInvoker, text_block
client = boto3.client(
"bedrock-runtime",
region_name="us-east-1",
aws_access_key_id="...",
aws_secret_access_key="...",
)
invoker = BedrockInvoker(
client_config=BedrockClientConfig(
region="us-east-1",
aws_access_key_id="...",
aws_secret_access_key="...",
),
client=client,
)
result = invoker.invoke_text(
model="arn:aws:bedrock:us-east-1:123456789012:inference-profile/example",
system_prompt="Answer briefly.",
user_content=[text_block("hello")],
)
Environment Variables
AWS / Bedrock
AWS_REGIONAWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY
Retry
BEDROCK_RETRY_MAX_ATTEMPTSBEDROCK_RETRY_BASE_SECONDSBEDROCK_RETRY_MAX_SECONDSBEDROCK_RETRY_JITTERBEDROCK_RETRYABLE_ERROR_CODES
The Bedrock SDK retry behavior is effectively disabled in the client, and the library uses its own manual progressive retry loop instead.
Default retryable error codes:
ServiceUnavailableExceptionInternalServerException
BEDROCK_RETRYABLE_ERROR_CODES is additive. If you set it, the provided codes
are added on top of those defaults rather than replacing them.
Default retry behavior:
BEDROCK_RETRY_MAX_ATTEMPTS=4BEDROCK_RETRY_BASE_SECONDS=5BEDROCK_RETRY_MAX_SECONDS=45BEDROCK_RETRY_JITTER=true
on_retry receives a typed RetryEvent with:
attemptmax_attemptserror_codeerrorsleep_secondsmodel_idelapsed_seconds
Prompt Cache
BEDROCK_PROMPT_CACHE_ENABLEDBEDROCK_PROMPT_CACHE_TTL
Public API
BedrockInvokerBedrockClientConfigRetryPolicyBedrockPromptCacheConfigInvokeResultRetryEventjson_text_blocktext_blockresolve_model_id
Model Resolution
If model is a full ARN, the library uses it directly.
If model is a shorthand like us.anthropic..., you must provide
profile_arn_fmt on the invoke call.
Testing
Run the test suite locally with:
PYTHONPATH=src python3 -m unittest discover -s tests -v
Examples
Runnable examples live in examples:
PYTHONPATH=src python3 examples/invoke_json_example.py
PYTHONPATH=src python3 examples/invoke_text_example.py
Consumer Integration
This repo includes library-level tests and Bedrock smoke examples.
Consumer repositories should still add their own integration smoke that uses their real prompt builders and payload-shaping logic through this library.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aws_bedrock_invoke-1.0.0.tar.gz.
File metadata
- Download URL: aws_bedrock_invoke-1.0.0.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ff5df4f380a61503de1094e8c048861fed33f6f5be5faa992aa6ae5ca875b493
|
|
| MD5 |
cd261895035dd78bcb989362bf9d80c3
|
|
| BLAKE2b-256 |
09f4aa087dbb9661559e9ea2844a0e1f6b77a8d69d8084901dc7e7489e1fcd95
|
File details
Details for the file aws_bedrock_invoke-1.0.0-py3-none-any.whl.
File metadata
- Download URL: aws_bedrock_invoke-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
33582b32e4c05996fe2eb6fbd10e08cc00c53e120cb6be663703d021568efc96
|
|
| MD5 |
2a8da1dd54116e0de654fdfa3b39cd80
|
|
| BLAKE2b-256 |
999f82a90b060ec1755abee5a77924d7576b997726979053d31d93c1a71be7c8
|