boto3 (Converse, InvokeModel)
pip install "getopentoken[bedrock]"With OT_INGEST_KEY and the environment set, every bedrock-runtime call is traced with model, tokens and cost:
import opentoken
opentoken.init()
import boto3
bedrock = boto3.client("bedrock-runtime")
reply = bedrock.converse(
modelId="us.amazon.nova-lite-v1:0",
messages=[{"role": "user", "content": [{"text": "Summarize our refund policy in one sentence."}]}],
)
print(reply["output"]["message"]["content"][0]["text"])Prefer not to touch the code? opentoken-instrument python app.py does the same (see SDK).
Bedrock AgentCore Runtime
AgentCore agents built with Strands, LangGraph or plain boto3 are containers: start them through the launcher and pass OT_INGEST_KEY (from Secrets Manager) and OTEL_EXPORTER_OTLP_ENDPOINT in the runtime's environment.
FROM public.ecr.aws/docker/library/python:3.12-slim
WORKDIR /app
COPY . .
RUN pip install --no-cache-dir "getopentoken[strands]" bedrock-agentcore
# OT_INGEST_KEY and OTEL_EXPORTER_OTLP_ENDPOINT come from the runtime's environment (Secrets Manager for the key).
CMD ["opentoken-instrument", "--service-name", "support-agent", "python", "agent.py"]AgentCore's own observability (CloudWatch) keeps working; spans also go to Open Token. If you'd rather not change the image at all, connect your AWS account and import them from CloudWatch (observe quickstart).
Bedrock Agents (managed)
Managed Bedrock Agents run inside AWS, so there's no process to instrument. Their model invocations are covered by the no-code import of Bedrock invocation logs (observe quickstart).
Bedrock-compatible endpoint
Open Token also speaks the Bedrock Runtime Converse API. Point any boto3 or AWS SDK client at it and use your Open Token API key as the Bedrock API key: budgets, limits and metering apply, and inference still runs in your AWS account with your connected credentials. Two environment variables route an existing app without code changes:
# Every boto3 / AWS SDK Bedrock client in this process now goes through Open Token, no code changes.
export AWS_ENDPOINT_URL_BEDROCK_RUNTIME="https://getopentoken.com/bedrock"
export AWS_BEARER_TOKEN_BEDROCK="$OT_API_KEY"Or per client:
import os
import boto3
os.environ["AWS_BEARER_TOKEN_BEDROCK"] = os.environ["OT_API_KEY"] # your Open Token API key is the Bedrock API key
bedrock = boto3.client("bedrock-runtime", region_name="us-east-1", endpoint_url="https://getopentoken.com/bedrock")
messages = [{"role": "user", "content": [{"text": "Say hello in one sentence."}]}]
reply = bedrock.converse(modelId="us.amazon.nova-lite-v1:0", messages=messages, requestMetadata={"feature": "support"})
print(reply["output"]["message"]["content"][0]["text"])
stream = bedrock.converse_stream(modelId="us.amazon.nova-lite-v1:0", messages=messages)
for event in stream["stream"]:
if "contentBlockDelta" in event:
print(event["contentBlockDelta"]["delta"]["text"], end="")
elif "metadata" in event:
print(f"\n{event['metadata']['usage']['totalTokens']} tokens")ConverseandConverseStream, text content. Tools, images, documents and guardrails answerValidationExceptionfor now;InvokeModelisn't available on this endpoint.- Model IDs: Bedrock model IDs, inference profile IDs or ARNs (
us.amazon.nova-lite-v1:0), or Open Token IDs. requestMetadatakeysfeature,customerandrun_idare forwarded to the gateway asX-OT-Feature,X-OT-CustomerandX-OT-Run-Id, the same attribution headers the SDK sends.- Errors use Bedrock's exception names (
AccessDeniedException,ThrottlingException,ValidationException…), so your retry logic keeps working. - This path has no fail-open: if Open Token is unreachable the call fails. For automatic fallback to Bedrock, use
wrap_bedrock()below. If the same process runsopentoken.init(), the SDK leaves Bedrock calls to this endpoint out of its instrumentation so they're counted once.
Route boto3 through the gateway (optional)
wrap_bedrock() keeps your client and your Converse calls; they go through the gateway for budgets and limits, and straight to Bedrock with your credentials if we're slow or down.
import boto3
import opentoken
opentoken.init(base_url="https://getopentoken.com/v1")
# Same boto3 client and Converse calls: routed through the gateway, straight to Bedrock if we're slow or down.
bedrock = opentoken.wrap_bedrock(boto3.client("bedrock-runtime"))
reply = bedrock.converse(
modelId="us.amazon.nova-lite-v1:0",
messages=[{"role": "user", "content": [{"text": "Say hello in one sentence."}]}],
)
print(reply["output"]["message"]["content"][0]["text"])