
Running pi on AWS: a durable agent on AgentCore for under a cent a turn
- Stephen Jones
- December 1, 2026
Table of Contents
Under a cent.
That’s what a turn cost when I put the pi agent harness on Amazon Bedrock AgentCore Runtime: a browser chat, a tool called through AgentCore Gateway, every step written to DynamoDB, and a conversation that survives the microVM underneath it being thrown away. One session, eight turns, ten model calls: $0.055, metered to the call. That’s about 0.7 cents a turn.
The number I didn’t expect was the one hiding inside it. A single tool call made every turn after it about seven times more expensive. I’ll come back to that, because it’s the most useful thing I learned.
Why pi, and why bother putting it in the cloud?
If you haven’t met it, pi is a small, opinionated agent harness. I run it on my laptop daily. It’s the thing I reach for when I want an agent loop without a framework sitting on top of it.
The laptop is the problem. A laptop agent dies when the lid closes, it holds my AWS credentials, and nobody else can talk to it. What I wanted was the same harness, unchanged, running somewhere managed, with a chat window anyone I authorise can open.
AgentCore Runtime looked like the right shape. Each session gets its own microVM, it speaks WebSocket, it validates a JWT before your code sees a byte, and you pay for active consumption rather than a server idling all night.
Then you hit the real wall.
AgentCore microVMs are disposable by design. Leave a session idle (15 minutes by default, 10 in my demo) and the microVM is stopped. Hit the maximum lifetime (8 hours by default, 60 minutes in my demo) and it’s gone. Any agent that keeps its conversation in memory forgets everything the moment that happens.
The shape of it
Here’s what I ended up with:
browser <agentcore-pi-chat>
| WebSocket, Cognito token in Sec-WebSocket-Protocol
v
AgentCore Runtime (Node 22, one microVM per session)
|-- pi-durable Harness --> Bedrock (Nova Pro), metered per call
|-- tools ------------------> AgentCore Gateway (MCP)
'-- storage ----------------> DynamoDB (+ S3 for big values)
Four pieces worth explaining: how the browser proves who it is, how the conversation survives, how the cost is capped, and what broke.
Identity Without a Relay
Browsers can’t set an Authorization header on a WebSocket. That’s the first thing that usually pushes people towards an API Gateway WebSocket relay in front of the agent.
You don’t need one. AgentCore accepts the token as a WebSocket subprotocol:
const ws = new WebSocket(url, [
`base64UrlBearerAuthorization.${base64UrlEncode(token)}`,
"base64UrlBearerAuthorization",
]);
AWS documents that half. What it doesn’t document is what your container sees when you combine it with a custom JWT authorizer and requestHeaderAllowlist: ["Authorization"]. I tested it rather than guessed: the container’s upgrade request arrives with Authorization: Bearer <the same token>, sub claim intact, matched by hash on both runs. A garbage token is refused before the upgrade with a 403 (the docs say 401).
So the agent knows exactly which user it’s talking to, and every DynamoDB item it writes sits under that user’s key.
Making the Conversation Outlive the microVM
This is where pi-durable comes in. It’s pi’s durable harness: every turn, tool call and partial answer is committed to storage before it’s shown, so a fresh process can call resume() and carry on mid-turn.
It ships no AWS storage, though. So I built one.
Before writing anything I ran a spike: DynamoDB, S3 Standard and S3 Express One Zone, all three put through pi-durable’s own conformance suite (33/33 each) and then measured inside an AgentCore microVM:
| DynamoDB (+ S3) | S3 Standard | S3 Express | |
|---|---|---|---|
| Commit p50 / p99 | 4.3 / 10.0 ms | 29.7 / 64.1 ms | 8.3 / 16.9 ms |
| Resume a 200-turn session | 57 ms | 255 ms | 45 ms |
| Cost per 1,000 turns | $0.048 | $0.109 | $0.046 |
S3 Express was close. It lost on something boring: in ap-southeast-2 it lives in one Availability Zone, and AgentCore put my microVMs in all three without letting me choose. DynamoDB won on commit latency (about six commits sit on every turn’s critical path), a well-understood fence, and native TTL. Cost didn’t separate them at all.
The result is @sjramblings/pi-durable-dynamodb, now on npm and on GitHub. Opening a session looks like this:
const storage = await openDynamoDBStorage({ client, s3, table, bucket, sessionId, ttl });
const harness = await Harness.open(storage, { models, registry: createRegistry() }, context);
Two things make it more than a key-value wrapper.
DynamoDB’s 400 KB item limit. One big tool result can blow straight through it. Anything over about 350 KB compressed spills to S3 and the item keeps a pointer. My first version of this project didn’t do that, and a single 500 KB entry was retried for ever while every later message queued behind it. That’s… not good.
Only one writer at a time. pi-durable’s README is blunt about it: there is no cross-process locking. If two microVMs open the same session, they’ll interleave and corrupt it. The package fences writers with conditional puts on sequence numbers, so when a second process takes over, the first one’s next commit throws and its harness stops.
The Bug I Didn’t See Coming
Here’s the one that made me sit back.
AgentCore restores microVMs from snapshots. That’s how it starts them quickly. Across about 30 sessions, crypto.randomUUID() produced four distinct values. Math.random(), the same. Every clone of the snapshot starts with the same random state.
My spike adapters drew their writer identity from randomUUID(). Two clones could each believe the other’s claim was their own, which breaks the fence in exactly the case it exists for.
crypto.getRandomValues() and randomBytes() differ per microVM, so writer identity now comes from those plus the runtime session id, and there’s a test that forces the other sources constant to prove it.
If you generate ids inside AgentCore with randomUUID(), go and check them. I’d bet money you’ll find duplicates.
Capping the Spend
An agent in the cloud with no spend limit is an invitation. Every session here has a ceiling ($0.50 by default), and every model call reserves its worst case before it’s sent: input estimate plus maxTokens, at the model’s price. If the reservation would cross the ceiling, the call is refused and never reaches Bedrock. When the call finishes, it settles to the actual usage.
Finding where to put that hook took a spike of its own. pi-durable has a beforeProviderRequest hook, but a hook that throws is skipped, so it can’t refuse anything. The answer was a metered Bedrock provider wrapped around the real one, the single seam every call passes through: normal turns, retries, compaction, the lot.
The same seam fixed a second bug. A run that pi-durable resumes on a fresh microVM never goes through the “user sent a prompt” path, so my credential setup never ran and the resumed call reached Bedrock without credentials. Credentials are now ensured inside the metered provider, before every call, however it started.
So Where Does the Money Go?
Back to that session. Here are its real calls, from the ledger in DynamoDB:
| Call | Input tokens | Output tokens | Cost |
|---|---|---|---|
| First call (“test”) | 547 | 127 | $0.0009 |
| After the tool returned 83 models | ~6,100 | ~40 | ~$0.0054 |
| Every later turn | ~7,300 | ~50 | ~$0.0064 |
| Session (10 calls) | 63,205 | 558 | $0.0550 |
Output is a rounding error. Input is 97% of the bill.
The tool call listed 83 Bedrock models. That result went into the conversation, and every call after it sent the whole conversation again, including roughly 5,500 tokens of model list nobody needed any more. One tool call, and the cost of every later turn went up about seven times.
That’s the difference between what a turn says and what a turn carries. If you’re estimating agent costs from output length, you’re estimating the wrong thing. Trim tool results before they enter the transcript, or summarise them, and you cut the bill far more than any model swap would.
Did It Actually Survive?
I didn’t want to claim resume works on the strength of a unit test, so I tested it on the deployed stack.
I asked it a question that used the tool. Then I closed the connection and left it alone for 11 minutes, past the 600-second idle timeout, so AgentCore recycled the microVM. Then I came back to the same session and asked: without calling any tool, what was the first word I sent you, and how many models did you report?
The first word you sent was “test”. You reported 83 models.
A new microVM, rebuilt from DynamoDB in milliseconds, picked up a conversation the old one had held. (It also said “you reported” when it meant “I reported”. Nova Pro, everyone.)
To make that visible, the chat window has a panel that reads the session straight from DynamoDB once a second. Send a message and you watch the user message, the cost reservation and pi-durable’s commits land in the table within a second, before the answer shows up beside them. The browser reads with its own short-lived credentials from a Cognito identity pool, and IAM only lets them query the signed-in person’s partition. Ask for anyone else’s and AWS says no.

If you would rather watch it than read it, here is the whole flow in a minute, with the real app in it:
The Honest Bits
A few real-world notes before you rush off to build this:
- pi-durable is experimental upstream. The package pins version 1.7.0 exactly, and it’s marked 0.x for the same reason. Expect the storage API to move.
- Nova Pro writes its reasoning as visible
<thinking>text. The chat page hides it now, but it’s still in the stored transcript and it still costs tokens. - A fenced writer can finish a tool already running. The fence stops a stale process at its next commit, not mid-tool. A tool with side effects can run twice during a takeover. The README says so.
- The $0.055 is model cost only. AgentCore Runtime compute, Gateway and DynamoDB are billed separately.
- You can’t SigV4 your way around a JWT runtime.
StopRuntimeSessionrefused my IAM-signed call with “Authorization method mismatch”. Testing a recycle meant waiting out the idle timeout.
The Bigger Picture
What struck me building this is how little of it was about the agent. pi didn’t change. The work was all in the layer underneath: who’s calling, where the conversation lives, who’s allowed to write it, and who pays.
That’s the layer that decides whether an agent is a demo or something you’d let other people use. AgentCore gives you a lot of it for free: isolation, identity, a managed tool gateway. It doesn’t give you durability or a spending limit, and it quietly hands you a snapshot that repeats its random numbers. You have to know to look.
Next up I’m adding sub-agents, one pi session fanning work out to others on their own microVMs, and then publishing the whole stack as a repo you can clone and deploy in an hour, teardown included. The storage package is already out there if you want to start with that.
I hope someone else finds this useful.
Cheers


