Amazon Bedrock AgentCore
A Strands agent served by Amazon Bedrock AgentCore Runtime. Strands traces every agent run, model call and tool call in GenAI semantic conventions through your OpenTelemetry tracer provider, and the snippet puts the AgentCore session id on each span as gen_ai.conversation.id. The same code runs locally and in the Runtime.
Oodle is a managed observability platform for metrics, logs, traces and agent traces. It ingests OpenTelemetry natively, so the snippet on this page is the complete setup.
Each trace shows the full transcript, the token counts and cost of every call, the agent structure (runs, steps and tool calls) and Signals: labels for loops, rate limits, refusals and tool failures, detected as the trace arrives.
Sign up for free to get an instance ID and an API key, or see the Agent Observability overview first.
You will need:
OODLE_INSTANCE: your Oodle instance ID (ap1, us1)OODLE_API_KEY: an Oodle API key (ap1, us1)OTLP_ENDPOINT: your OTLP collector domain, shown on the tile
Open the Amazon Bedrock AgentCore tile on the ap1, us1 page to
get these filled in for you, or let an agent do the setup with
/oodle-onboarding set up the llm_observability_agentcore integration.
Install
Add these to the requirements.txt you deploy, and install them for a local run. Leave aws-opentelemetry-distro out: with it, AgentCore starts the agent under ADOT, which takes the tracer provider:
pip install strands-agents bedrock-agentcore \
opentelemetry-sdk \
opentelemetry-exporter-otlp-proto-http
Instrument
Set up OpenTelemetry before the first Strands agent is built, and flush before each invocation returns. Strands then traces every agent run, model call and tool call in GenAI semantic conventions, with the AgentCore session id as the conversation id:
import os
import uuid
from bedrock_agentcore.runtime import BedrockAgentCoreApp
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from strands import Agent, tool
from strands.models import BedrockModel
def setup_opentelemetry():
resource = Resource.create({"service.name": "my-agentcore-agent"})
tracer_provider = TracerProvider(resource=resource)
# AgentCore Runtime sets OTEL_EXPORTER_OTLP_TRACES_HEADERS for its own
# CloudWatch export, and that variable wins over
# OTEL_EXPORTER_OTLP_HEADERS. Pass the Oodle headers here instead.
exporter = OTLPSpanExporter(
headers={
"X-API-KEY": os.environ["OODLE_API_KEY"],
"X-OODLE-INSTANCE": os.environ["OODLE_INSTANCE"],
}
)
tracer_provider.add_span_processor(BatchSpanProcessor(exporter))
trace.set_tracer_provider(tracer_provider)
return tracer_provider
# Before the first Agent: Strands traces through the global provider.
tracer_provider = setup_opentelemetry()
@tool
def get_weather(city: str) -> str:
"""Return the weather for a city."""
return f"Sunny in {city}"
app = BedrockAgentCoreApp()
@app.entrypoint
def invoke(payload, context):
session_id = context.session_id or str(uuid.uuid4())
agent = Agent(
name="weather-agent",
model=BedrockModel(model_id="us.amazon.nova-lite-v1:0"),
system_prompt="Use tools when asked about the weather.",
tools=[get_weather],
callback_handler=None,
# Groups every turn of an AgentCore session in Oodle.
trace_attributes={"gen_ai.conversation.id": session_id},
)
try:
result = agent(payload.get("prompt", "What is the weather in Paris?"))
return {"result": str(result)}
finally:
# The Runtime can pause the process once it has answered.
# Send this turn's spans before returning.
tracer_provider.force_flush()
if __name__ == "__main__":
app.run()
Environment
Point the application at Oodle:
# Set these for a local run, and pass the same variables to the
# AgentCore Runtime as environment variables when you deploy.
# OTLP endpoint (points straight at Oodle)
export OTEL_EXPORTER_OTLP_ENDPOINT=https://<OTLP_ENDPOINT>
# Passed to the exporter in code: the Runtime overrides the
# OTLP headers variables for its own CloudWatch export.
export OODLE_API_KEY=<OODLE_API_KEY>
export OODLE_INSTANCE=<OODLE_INSTANCE>
# Compress the export: prompt payloads are large
export OTEL_EXPORTER_OTLP_COMPRESSION=gzip
# GenAI semantic conventions, with prompts and replies on the spans
export OTEL_SEMCONV_STABILITY_OPT_IN=gen_ai_latest_experimental,gen_ai_span_attributes_only
What each invocation becomes
| Strands activity | Span |
|---|---|
| One invocation of the entrypoint | invoke_agent <agent>, the root of the trace |
| One reasoning cycle | execute_event_loop_cycle |
| A model call | chat, with the messages, system instructions and token counts |
| A tool call | execute_tool <tool>, with its arguments and result |
Each invocation is one trace. Every turn of one AgentCore session
carries the same gen_ai.conversation.id, so the Sessions view groups
them. The invoke_agent span repeats the token totals of its chat
spans; only the chat spans are counted, so totals are not doubled.
Deploying to AgentCore Runtime
Deploy with the AgentCore CLI as you do now. Two things decide whether traces leave the Runtime:
- Pass the environment variables to the Runtime. It does not see your shell. Set every variable from the Environment step as a Runtime environment variable at deploy time.
- Keep the Oodle headers in code. The Runtime sets
OTEL_EXPORTER_OTLP_TRACES_HEADERSfor its own CloudWatch export, and the exporter prefers it toOTEL_EXPORTER_OTLP_HEADERS. The snippet passes the headers toOTLPSpanExporterfromOODLE_API_KEYandOODLE_INSTANCE; in the generic variable they are dropped and Oodle refuses every export with 401. - Keep
aws-opentelemetry-distroout ofrequirements.txt, and anyopentelemetry-instrumentation-*package with it. With either there, AgentCore starts the agent asopentelemetry-instrument main.py. ADOT then owns the tracer provider, the one the snippet sets is refused, and spans go wherever ADOT is configured to send them.
The Runtime can pause the process as soon as the entrypoint returns, so
the snippet flushes the spans in a finally block first. Without it a
turn waits in the batch until the next invocation.
Through an OpenTelemetry Collector, the Runtime has to reach the collector: in public network mode it cannot reach a private address, so use VPC network mode or export direct to Oodle.
Content
Strands puts prompts, replies, system instructions and tool arguments
and results on the spans with the opt-in shown. To redact them, add
gen_ai_unredacted_attributes= to OTEL_SEMCONV_STABILITY_OPT_IN:
Strands then replaces every content attribute it would send. Token
counts, timings, model and tool names, and errors remain.
Verify
Run your application, then open ap1, us1. Spans
carry gen_ai.* attributes: the model, token counts, and the prompt and
response content. Click a trace for the Transcript, the waterfall, and
the cost breakdown.
If nothing arrives, check that the exporter can reach
https://<OTLP_ENDPOINT> and that the instance and key are set: the
OTLP gateway answers 401 without them.
Support
If you need assistance or have any questions, please reach out to us through:
- Email at [email protected]