rig-agentcore
Run a Rig agent on AWS Bedrock AgentCore Runtime.
AgentCore runs an agent as an ARM64 container serving its HTTP protocol contract on port
8080. AgentCoreApp serves it for a Rig agent:
| Path | What it does |
|---|---|
POST /invocations |
Takes {"prompt": "..."}. Answers {"response": "...", "status": "success"}, or server-sent events ({"text"} as the model writes, then {"response"}) when the caller accepts text/event-stream. |
GET /ping |
{"status": "Healthy"}, or "HealthyBusy" while work is open, with time_of_last_update. |
GET /ws |
WebSocket. Each text message takes the /invocations body, and the reply comes back as frames. See WebSocket (/ws). |
use Agent;
use AgentCoreApp;
async
-
Sessions: the
X-Amzn-Bedrock-AgentCore-Runtime-Session-Idheader names the conversation; each session's messages are kept and sent with its next invocation. By default they live in memory (at most 1000 sessions, oldest forgotten first).history_storekeeps them in anyrig-memorystore (DirStoreon disk,rig-sqlite,rig-postgres), so they survive a restart and several apps can share them. Any session id works: one of 1-128 of[A-Za-z0-9_-]names its log as it is, any other is hashed into a safe log name. A save that loses a race with another writer retries up to 10 times; if it still fails, the answer is returned, the failure is logged, and that turn is not persisted. A failed load is an error reported before the agent runs. Each invocation sends the newest messages that fithistory_window(32k tokens by default,rig_memory::recent::TokenBudget), never splitting a tool call from its result. An empty window sends no history. In memory, messages beyond the window are forgotten when a run is saved. Withhistory_store, the store keeps the whole conversation and the window only limits what is sent.use Agent; use AgentCoreApp;DirStoreneeds rig-memory'sfsfeature: enable it in your ownCargo.toml(rig-memory = { version = "0.47", features = ["fs"] }). -
Summaries:
summarize_history(summarizer, keep_recent, trigger_messages)makes a long session shorter. Once a session holds more thantrigger_messagesmessages, an agent without tools (summarizer) summarizes all but the newestkeep_recent, and that one summary becomes the session's first message. The cut never separates a tool call from its result. If the summary fails, a warning is logged, the session keeps its full history, and the invocation answers as usual. Withhistory_store, the summary is written to the store in place of the messages it replaces. Rig-memory has no summarizing window (its compaction is a planning API the caller drives), so this option is built on its compaction helpers.Each summary attempt is traced as an
agentcore.summarizespan under the invocation span. It records the messages summarized and kept, the outcome (summarized,skipped_changed,skipped_nothing_olderorfailed), and the summarizer's token usage. The prompt and the summary text are recorded only withrecord_content. Success is also an info event, and a failure keeps its warning.use Agent; use AgentCoreApp; -
Traces: each invocation runs in an
agentcore.invocationspan that continues the caller's trace (traceparent) and gives every span under itsession.id, throughrig-otel. With rig-otel'sawsfeature exporting to CloudWatch, that is what AgentCore Observability and AgentCore Evaluations read. To see the spans of theserveexample on your machine, setOTEL_EXPORTER_OTLP_ENDPOINT(for examplehttp://127.0.0.1:4318) and run it against a collector. -
Limits: all opt-in.
max_concurrent_taskscaps invocations running at once (past the cap, 429 before the agent runs; no cap by default, since AgentCore Runtime gives each session its own microVM).max_run_durationstops an invocation, summarizing included, after a time (504, or an{"error"}event when streaming, and its turn is not kept; no limit by default).max_request_bytessets the body limit (default 2 MiB, answered with 413). Outside AgentCore Runtime, a cap around 32 and a ten-minute run limit suit most agents.use Duration; use Agent; use AgentCoreApp; -
Background work:
/pinganswersHealthyBusywhile an invocation is running, and while work fromspawn_taskor atrack_taskguard is open. AgentCore Runtime keeps a session running while it reportsHealthyBusy, so a background task outlives the invocation that started it. The status returns toHealthywhen the work ends.use AgentCoreApp; -
Content:
record_content(true)records prompts and replies on the GenAI spans. AgentCore Evaluations needs them to judge a session. They are off by default, as in Rig. The setting applies to every invocation and always overridesAgentBuilder::record_content_telemetryon the agent. The agent's setting is ignored when it is served throughAgentCoreApp, so to record content, call.record_content(true)on the app. This is planned to change in 0.48 (PR #42), so that an app setting left unset keeps the agent's.
WebSocket (/ws)
/ws is the contract's WebSocket endpoint, served on the same port beside /invocations
and /ping. It needs no setting.
- Upgrade
GET /ws. The session header (X-Amzn-Bedrock-AgentCore-Runtime-Session-Id) names the session. Without it, a message'ssession_iddoes, or the connection gets its own. - Only JSON text messages are accepted: an object with a
prompt, as the/invocationsbody is. The contract also lists plain text and binary "based on application requirement"; this crate does not read them, and such a message gets an error frame. - Replies are frames:
{"text", "session_id", "message_type": "agent_text"}as the model writes, then{"response", "session_id", "message_type": "agent_response"}. Errors are{"error", "status": "error", "message_type": "error"}. - A connection has one run in flight. A message sent during a run gets a busy error frame. Pings are answered during a run.
- A message over
max_request_bytesgets an error frame, and the connection closes. - A client that disconnects cancels the agent's run. A reply that has finished is still saved.
- Open sockets are not capped and have no idle timeout. Bound the runs with
max_concurrent_tasksandmax_run_duration.
Frame limits. AgentCore Runtime limits a WebSocket frame to 64 KB and a connection to 250 frames per second. The troubleshooting page says the connection closes when these are exceeded. So:
- A prompt over 64 KB cannot be sent in one frame.
max_request_bytes(2 MB by default) does not lift the AWS limit. - Text deltas go out at most once per 10 ms, so a fast model stays under 100 text frames a second, well inside 250. Deltas are gathered, not dropped.
- A final reply over 64 KB gets an error frame instead of the answer. Its text was already
streamed in
agent_textframes, or use/invocationsfor it.
The AWS pages give no close codes or ping/pong rules for /ws; the WebSocket defaults apply.
Verified on AWS
On 2026-11-26, an agent built with this crate ran on Amazon Bedrock AgentCore Runtime in us-east-1:
- It was deployed as an ARM64 container image from Amazon ECR, with an execution role.
- A two-turn session kept its conversation across invocations.
- Its traces reached CloudWatch (
aws/spans), with the session id on every span. The agent usedrecord_content(true)and rig-otel'sawsexport. - AgentCore Evaluations' built-in evaluators scored the sessions from those spans: GoalSuccessRate 1.0, Helpfulness 0.83 and Correctness 1.0 per turn.
That agent was a private application that used a calculator tool and a Bedrock model. The
steps below use the public serve example instead. They reproduce the deployment and
invocation. The example does not export traces, so step 6 adds that to your own agent.
Set these once. Replace the placeholders with your values:
-
Build the ARM64 image from the repository root. Docker needs buildx. On an x86 host the build runs under emulation and is slow.
-
Create an ECR repository and push the image:
| \ -
Create an execution role for the runtime. AWS documents the required permissions in IAM permissions for AgentCore Runtime. Save the trust policy as
trust.json:Save this permissions policy as
execution.json. It covers pulling the image, writing logs, and sending traces and metrics (the statements AWS lists for the runtime execution role). Addbedrock:InvokeModelandbedrock:InvokeModelWithResponseStreamfor your model only if your agent calls Bedrock: -
Create the runtime with the AWS CLI. Its
helpoutput lists every option:aws bedrock-agentcore-control create-agent-runtime help. Or use the console, under Amazon Bedrock AgentCore. The developer guide is at https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/.The runtime starts in
CREATING. Wait untilstatusisREADY, then note its ARN:The example serves
/pingand/invocationson port 8080, as the HTTP protocol contract requires. -
Invoke the runtime. The session id must be at least 33 characters, and
uuidgengives 36. The output goes toout.json:Save the request body as
payload.json:Send a second call with the same
--runtime-session-idand a prompt such asWhat is my name?. The runtime's logs show that the second request carries the first turn's messages. WithoutRIG_MODEL(orOPENROUTER_MODEL) and its key in the runtime's environment, the example answers with a scripted model. Keep API keys out of plain environment variables: use AWS Secrets Manager. -
Enable observability. Turn on CloudWatch Transaction Search once per account. AWS documents the steps in Enable transaction search. Create the resource policy from that page (
aws logs put-resource-policy), then run:Then add traces to your agent. Serve it through
AgentCoreApp::new(agent).record_content(true), since the app's setting overrides the agent'srecord_content_telemetry. Callrig_otel::Telemetry::builder().aws_cloudwatch().await, and setOTEL_RESOURCE_ATTRIBUTES=service.name=<name>,aws.log.group.names=/aws/bedrock-agentcore/runtimes/<runtime-id>-DEFAULTon the runtime, asrig-oteldocuments. AgentCore Observability and Evaluations then read the spans. -
Clean up when you are done:
Testing
cargo test serves the contract over real HTTP for agents on scripted models: ping (with
busy status while work is open), a
session keeping its conversation, sessions kept apart, streamed replies, a bad body,
the run and body limits, and an invocation continuing the caller's trace with the session on every span.