Skip to content

Commit 2878735

Browse files
committed
add AWS AgentCore zero code example
1 parent da3ca5e commit 2878735

3 files changed

Lines changed: 112 additions & 1 deletion

File tree

examples/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ agentevals accepts OTLP/HTTP on port 4318 (`http/protobuf` and `http/json`) and
3030
| [zero-code-examples/strands/](./zero-code-examples/strands/) | Strands | OpenAI |
3131
| [zero-code-examples/adk/](./zero-code-examples/adk/) | Google ADK | Gemini |
3232
| [zero-code-examples/pydantic-ai/](./zero-code-examples/pydantic-ai/) | Pydantic AI | OpenAI |
33+
| [zero-code-examples/agentcore/](./zero-code-examples/agentcore/) | AWS AgentCore | Amazon Bedrock |
3334

3435
This approach works with any framework that has OTel instrumentation: LangChain, Strands, Google ADK, etc. If your framework already emits OTel spans, you only need to add `OTLPSpanExporter` (and `OTLPLogExporter` if it uses GenAI log-based content delivery).
3536

@@ -105,6 +106,7 @@ Detection checks for `gen_ai.request.model` / `gen_ai.input.messages` (GenAI sem
105106
| [zero-code-examples/strands/](./zero-code-examples/strands/) | Strands | OpenAI | GenAI semconv (events*) | Standard OTLP export |
106107
| [zero-code-examples/adk/](./zero-code-examples/adk/) | Google ADK | Gemini | ADK built-in | Standard OTLP export |
107108
| [zero-code-examples/pydantic-ai/](./zero-code-examples/pydantic-ai/) | Pydantic AI | OpenAI | GenAI semconv (span attrs) | Standard OTLP export |
109+
| [zero-code-examples/agentcore/](./zero-code-examples/agentcore/) | AWS AgentCore | Amazon Bedrock | GenAI semconv (events*) | Standard OTLP export |
108110
| [langchain_agent](./langchain_agent/) | LangChain | OpenAI | GenAI semconv (logs) | SDK WebSocket |
109111
| [strands_agent](./strands_agent/) | Strands | OpenAI | GenAI semconv (events*) | SDK WebSocket |
110112
| [dice_agent](./dice_agent/) | Google ADK | Gemini | ADK built-in | SDK WebSocket |
@@ -220,6 +222,7 @@ python examples/zero-code-examples/ollama/run.py
220222
python examples/zero-code-examples/strands/run.py
221223
python examples/zero-code-examples/adk/run.py
222224
python examples/zero-code-examples/pydantic-ai/run.py
225+
python examples/zero-code-examples/agentcore/run.py
223226

224227
# SDK examples:
225228
python examples/sdk_example/context_manager_example.py
@@ -235,7 +238,7 @@ python examples/strands_agent/main.py
235238
Traces stream to the dev server in real-time. Evaluation runs automatically when the session completes.
236239

237240
See each example's README for prerequisites and detailed instructions:
238-
- [zero-code-examples/](./zero-code-examples/) (LangChain, Strands, ADK, OpenAI Agents, Pydantic AI — standard OTLP)
241+
- [zero-code-examples/](./zero-code-examples/) (LangChain, Strands, ADK, OpenAI Agents, Pydantic AI, AWS AgentCore — standard OTLP)
239242
- [dice_agent/README.md](./dice_agent/README.md) (Google ADK + Gemini)
240243
- [langchain_agent/README.md](./langchain_agent/README.md) (LangChain + OpenAI, SDK)
241244
- [strands_agent/](./strands_agent/) (Strands + OpenAI, SDK)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
strands-agents[bedrock]>=0.1.0
2+
3+
opentelemetry-sdk>=1.36.0
4+
opentelemetry-exporter-otlp-proto-http>=1.36.0
5+
python-dotenv>=1.0.0
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"""Run a dice agent using AWS AgentCore with OTLP export.
2+
3+
Shows how to send AgentCore traces to agentevals without using the
4+
agentevals SDK. The OTLP exporter points at the local receiver endpoint.
5+
6+
AgentCore runs on Strands Agents with Amazon Bedrock models. This example
7+
sets up OTel via StrandsTelemetry and adds the OTLP exporter to the tracer
8+
provider before running the agent.
9+
10+
Prerequisites:
11+
1. pip install -r requirements.txt
12+
2. agentevals serve --dev
13+
3. AWS credentials configured (aws configure, env vars, or IAM role)
14+
4. Amazon Nova Pro model access enabled in your AWS account
15+
16+
Usage:
17+
python examples/zero-code-examples/agentcore/run.py
18+
"""
19+
20+
import os
21+
import random
22+
23+
from dotenv import load_dotenv
24+
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
25+
from opentelemetry.sdk.trace.export import BatchSpanProcessor
26+
from strands import Agent, tool
27+
from strands.models import BedrockModel
28+
from strands.telemetry import StrandsTelemetry
29+
30+
load_dotenv(override=True)
31+
32+
os.environ.setdefault("OTEL_SEMCONV_STABILITY_OPT_IN", "gen_ai_latest_experimental")
33+
34+
35+
@tool
36+
def roll_die(sides: int = 6) -> dict:
37+
"""Roll a die with the given number of sides."""
38+
result = random.randint(1, sides)
39+
return {
40+
"sides": sides,
41+
"result": result,
42+
"message": f"Rolled a {sides}-sided die and got {result}",
43+
}
44+
45+
46+
@tool
47+
def check_prime(number: int) -> bool:
48+
"""Return True if number is prime."""
49+
if number < 2:
50+
return False
51+
for i in range(2, int(number**0.5) + 1):
52+
if number % i == 0:
53+
return False
54+
return True
55+
56+
57+
def main():
58+
if not os.getenv("AWS_DEFAULT_REGION"):
59+
print("AWS_DEFAULT_REGION not set.")
60+
return
61+
62+
endpoint = os.environ.get("OTEL_EXPORTER_OTLP_ENDPOINT", "http://localhost:4318")
63+
print(f"OTLP endpoint: {endpoint}")
64+
65+
os.environ.setdefault(
66+
"OTEL_RESOURCE_ATTRIBUTES",
67+
"agentevals.eval_set_id=agentcore_eval,agentevals.session_name=agentcore-zero-code",
68+
)
69+
70+
telemetry = StrandsTelemetry()
71+
telemetry.tracer_provider.add_span_processor(
72+
BatchSpanProcessor(OTLPSpanExporter(), schedule_delay_millis=1000)
73+
)
74+
75+
agent = Agent(
76+
model=BedrockModel(model_id="us.amazon.nova-pro-v1:0"),
77+
tools=[roll_die, check_prime],
78+
system_prompt=(
79+
"You are a helpful assistant that can roll dice and check if numbers are prime. "
80+
"Use roll_die with the right number of sides when asked. "
81+
"Use check_prime when asked about prime numbers."
82+
),
83+
name="dice_agent",
84+
)
85+
86+
test_queries = [
87+
"Hi! Can you help me?",
88+
"Roll a 20-sided die for me",
89+
"Is the number you rolled prime?",
90+
]
91+
92+
for i, query in enumerate(test_queries, 1):
93+
print(f"\n[{i}/{len(test_queries)}] User: {query}")
94+
result = agent(query)
95+
print(f" Agent: {result}")
96+
97+
print()
98+
telemetry.tracer_provider.force_flush()
99+
print("All traces flushed to OTLP receiver.")
100+
101+
102+
if __name__ == "__main__":
103+
main()

0 commit comments

Comments
 (0)