View a markdown version of this page

Run your first agent on Amazon Bedrock AgentCore Runtime - Amazon Bedrock

Run your first agent on Amazon Bedrock AgentCore Runtime

This tutorial creates an agent, runs a session on it whose tools run on your AgentCore Runtime, and reads what the agent did. It uses curl and jq, so you can see each request.

To create the same agent and session in the Amazon Bedrock console instead, see Use the Amazon Bedrock console.

Complete Set up permissions and prerequisites first, including the AgentCore Runtime. You need its ARN, which is the RuntimeArn output of the CloudFormation stack. To run the tools on your own machine instead, see Run the tool runner in a self-hosted environment.

Step 1: Set up a shell

Use a profile for a role that has the ClaudeManagedAgentsCaller policy. The API refuses requests signed by an IAM user or the account root, so the profile must give you role credentials.

export AWS_PROFILE=<a profile for a role with ClaudeManagedAgentsCaller> export AWS_REGION=us-west-2 export CMA_ENDPOINT_URL=https://bedrock.us-west-2.amazonaws.com/anthropic/v1 export RUNTIME_ARN=<the RuntimeArn output of your stack> eval "$(aws configure export-credentials --format env)" cma() { local method=$1 route=$2 shift 2 printf 'user = "%s:%s"\nheader = "x-amz-security-token: %s"\n' \ "$AWS_ACCESS_KEY_ID" "$AWS_SECRET_ACCESS_KEY" "$AWS_SESSION_TOKEN" | curl -sS --config - -X "$method" --aws-sigv4 "aws:amz:${AWS_REGION}:bedrock" \ -H "anthropic-version: 2023-06-01" \ -H "anthropic-beta: managed-agents-2026-04-01" \ -H "content-type: application/json" \ "$@" "${CMA_ENDPOINT_URL}/${route}" }

The function passes your credentials to curl on stdin, so they don't appear in the process list or your shell history. The exported credentials last as long as the role session. When they expire, run the eval line again.

The function works in bash and zsh. Don't rename route to path: in zsh, path is tied to PATH, so the function could no longer find curl.

Step 2: Create an agent

An agent holds the model, the system prompt, and the tools. agent_toolset_20260401 turns on the built-in tools, such as bash and the file tools, and always_allow lets them run without asking.

AGENT_ID=$(cma POST agents -d '{ "name": "first-agent", "execution_identity": {"type": "aws_role", "role_arn": "arn:aws:iam::111122223333:role/ClaudeManagedAgentsSessionRole"}, "model": "us.anthropic.claude-sonnet-5-5", "system": "You are a careful assistant. Use bash to answer questions about the machine you run on.", "tools": [{"type": "agent_toolset_20260401", "default_config": {"permission_policy": {"type": "always_allow"}}}] }' | jq -r .id) echo "$AGENT_ID"

The response is the stored agent, at version 1. Configuration changes create new versions. An update that changes nothing, or only sets the role, does not create a new version.

Step 3: Create a session

SESSION_ID=$(cma POST sessions -d "{ \"agent\": \"${AGENT_ID}\", \"environment_id\": \"${RUNTIME_ARN}\" }" | jq -r .id) echo "$SESSION_ID"

The session inherits the agent's role. To run one session as another role, add "execution_identity" to the session create. Your caller needs iam:PassRole on that role. To change the role for every new session, update the agent's role. Your caller needs iam:PassRole on it, and sessions that already exist keep their role. Either way, give the role the tag ManagedAgentsExecutionRole with the value enabled. Creating a session starts nothing yet. The service starts the session's container on your runtime when you send the first message.

Step 4: Watch the session

In a second terminal, set up the same shell as in Step 1, set SESSION_ID, and open the event stream:

cma GET "sessions/${SESSION_ID}/events/stream" -N

The stream starts with : connected. Each event arrives as an event: message block whose data line is one JSON object. Leave it running.

Step 5: Send a message

In the first terminal:

cma POST "sessions/${SESSION_ID}/events" -d '{ "events": [{"type": "user.message", "content": [{"type": "text", "text": "Run uname -a and tell me what it says."}]}] }' | jq .

The first message can take several seconds to return, because the service starts the container before it accepts the message. The response lists the events it stored. It does not wait for the turn to finish.

In the stream you see session.status_running, then agent.tool_use for bash, then user.tool_result with the command's output from your tool runner, then agent.message with the agent's reply, and then session.status_idle. Events about threads, such as session.thread_status_running, and model request spans come in between.

Note

These events follow the flow in How it works: the service started your container and called Claude as your session role, Claude chose bash, your tool runner ran the command, and the session went idle.

Step 6: Read the result

The stream shows events as they happen. To read them afterward, list them:

cma GET "sessions/${SESSION_ID}/events" \ | jq -r '.data[] | [.type, (.name // ""), (.content // "" | tostring)[0:120]] | @tsv'

The turn is over when session.status_idle arrives. Its stop_reason.type says why:

stop_reason.type

What it means

end_turn

The agent finished and waits for your next message.

requires_action

The agent is waiting on you, for example to allow a tool call. See Work with sessions, events, and results.

retries_exhausted

The service retried a failure, such as a model request, and gave up. The session still works. Send another message to try again.

An idle session is not proof that the task worked. Read the tool results and the agent's message.

Step 7: Continue the conversation

The session keeps the conversation, so a second message can refer to the first:

cma POST "sessions/${SESSION_ID}/events" -d '{ "events": [{"type": "user.message", "content": [{"type": "text", "text": "Which CPU architecture was that?"}]}] }' > /dev/null

Send the next message after the turn ends. Work with sessions, events, and results covers interrupting a turn and answering tool calls.

Step 8: Delete the session

Deleting a session removes its conversation and events from the service. It does not delete the agent or files that the tools wrote in your runtime.

cma DELETE "sessions/${SESSION_ID}" | jq .

The service stops the session's container. A session cannot be deleted while it is running; interrupt it first. Stop the stream in the second terminal with Ctrl+C.

The agent stays, and you can create more sessions on it. To retire it, archive it with cma POST "agents/${AGENT_ID}/archive". An archived agent cannot start new sessions.

Next steps

After your first agent works, continue with these topics: