# Amazon Bedrock AgentCore

AWS offers AgentCore as part of the Bedrock suite, giving teams a production-ready infrastructure for running AI agents. The service handles containerization, tool orchestration, and model access through a unified runtime that connects to Bedrock's foundation model catalog.

This guide covers instrumenting AgentCore agents with InteractiveAI via OpenTelemetry for full observability into agent execution, model interactions, and tool usage.

### Prerequisites

* InteractiveAI account with API credentials
* AWS account with Bedrock access

***

### Installation

Install the required packages:

```bash
pip install bedrock-agentcore-starter-toolkit strands-agents[otel] interactiveai boto3 mcp
```

***

### Configuration

#### InteractiveAI and OpenTelemetry Setup

Configure your InteractiveAI credentials and prepare the OpenTelemetry exporter:

```python
import os
import base64
from interactiveai import Interactive

# InteractiveAI credentials
os.environ["INTERACTIVEAI_PUBLIC_KEY"] = "pk-ia-..."
os.environ["INTERACTIVEAI_SECRET_KEY"] = "sk-ia-..."
os.environ["INTERACTIVEAI_HOST"] = "https://app.interactiveai.com"
interactiveai = Interactive(
    secret_key=os.getenv("INTERACTIVEAI_SECRET_KEY"),
    public_key=os.getenv("INTERACTIVEAI_PUBLIC_KEY")
)

# Construct Basic Auth header for OTLP export
auth_string = base64.b64encode(
    f"{os.environ['INTERACTIVEAI_PUBLIC_KEY']}:{os.environ['INTERACTIVEAI_SECRET_KEY']}".encode()
).decode()

# Configure OpenTelemetry exporter
os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = os.environ["INTERACTIVEAI_HOST"] + "/api/public/otel"
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"Authorization=Basic {auth_string}"
```

#### AWS Credentials

Configure access to Amazon Bedrock services:

```python
os.environ["AWS_ACCESS_KEY_ID"] = "..."
os.environ["AWS_SECRET_ACCESS_KEY"] = "..."
os.environ["AWS_DEFAULT_REGION"] = "us-west-2"
```

***

### Creating an Instrumented Agent

Build an AgentCore agent with InteractiveAI tracing enabled through OpenTelemetry. This example uses Strands Agents SDK with MCP tools, though any compatible agent framework can be instrumented similarly.

```python
from bedrock_agentcore.runtime import BedrockAgentCoreApp
from strands import Agent
from strands.models import BedrockModel
from strands.telemetry import StrandsTelemetry
from mcp.client.streamable_http import streamablehttp_client
from strands.tools.mcp.mcp_client import MCPClient
from interactiveai import Interactive

# Initialize MCP client for external tool access
mcp_client = MCPClient(
    lambda: streamablehttp_client("https://your-mcp-endpoint.com/api/mcp")
)

# Configure foundation model
bedrock_model = BedrockModel(
    model_id="us.anthropic.claude-3-7-sonnet-20250219-v1:0",
    region_name="us-west-2",
    temperature=0.0,
    max_tokens=4096
)

system_prompt = """You are a knowledgeable assistant helping developers 
understand AI observability and agent monitoring best practices."""

app = BedrockAgentCoreApp()

@app.entrypoint
def agent_handler(payload):
    """Primary agent entrypoint with trace instrumentation"""
    user_input = payload.get("prompt")
    trace_id = payload.get("trace_id")
    parent_obs_id = payload.get("parent_obs_id")

    # Initialize telemetry and configure OTLP export
    telemetry = StrandsTelemetry()
    telemetry.setup_otlp_exporter()

    # Build agent with MCP tools
    with mcp_client:
        available_tools = mcp_client.list_tools_sync()

        agent = Agent(
            model=bedrock_model,
            system_prompt=system_prompt,
            tools=available_tools
        )

        # Execute within InteractiveAI trace context
        client = Interactive()
        with client.start_as_current_span(
            name="bedrock-agentcore-execution",
            trace_context={
                "trace_id": trace_id,
                "parent_observation_id": parent_obs_id
            }
        ):
            response = agent(user_input)

    return response.message["content"][0]["text"]

if __name__ == "__main__":
    app.run()
```

***

### Deployment and Invocation

Deploy the agent to Amazon Bedrock AgentCore and invoke it with trace context propagation:

```python
from bedrock_agentcore_starter_toolkit import Runtime
from interactiveai import Interactive
import boto3
import json

# Deploy agent to AgentCore
runtime = Runtime()
runtime.configure(
    entrypoint="./agent.py",
    auto_create_execution_role=True,
    auto_create_ecr=True,
    agent_name="traced-agentcore-agent",
    memory_mode="NO_MEMORY"
)

deployment = runtime.launch(
    env_vars={
        "BEDROCK_MODEL_ID": "us.anthropic.claude-3-7-sonnet-20250219-v1:0",
        "OTEL_EXPORTER_OTLP_ENDPOINT": os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"],
        "OTEL_EXPORTER_OTLP_HEADERS": os.environ["OTEL_EXPORTER_OTLP_HEADERS"],
        "SYSTEM_PROMPT": system_prompt
    }
)

# Invoke agent with trace propagation
bedrock_client = boto3.client("bedrock-agentcore", region_name="us-west-2")

# Retrieve current trace context
client = Interactive()
current_trace_id = client.get_current_trace_id()
current_obs_id = client.get_current_observation_id()

request_payload = json.dumps({
    "prompt": "How do I implement comprehensive monitoring for production AI agents?",
    "trace_id": current_trace_id,
    "parent_obs_id": current_obs_id
}).encode()

response = bedrock_client.invoke_agent_runtime(
    agentRuntimeArn=deployment.agent_arn,
    runtimeSessionId="session-123",
    payload=request_payload
)
```

***

### Viewing Trace Data

After agent execution, the InteractiveAI dashboard displays complete trace information including:

* End-to-end agent execution flow
* Individual model calls with token consumption and cost calculations
* Tool invocations and MCP interactions
* Latency measurements for each operation
* Request and response payloads for debugging

These traces provide visibility into agent behavior across development, staging, and production environments.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.interactive.ai/integrations/ai-frameworks/amazon-bedrock-agentcore.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
