Three Async Patterns Cut Lambda Idle Cost in Bedrock AgentCore Pipelines

Published Β· Updated β€’ news
Amazon BedrockServerlessAI Agents

AWS authors Daniel Abib and Alexandre Farber describe three ways to avoid holding a Lambda caller open while an AgentCore agent works: task-token callbacks, direct integration and durable-function suspension. Their 19 August post uses a deliberately synthetic document pipeline. Its code and duration readings are examples, not an AI Mastery deployment or a general benchmark.

Correction, 22 October 2026: The earlier report called the examples production patterns without identifying simulated extraction, omitted AgentCore's memory charge during some idle periods and described a 19.6-second state duration as the gap between two rounded timestamps about 15 seconds apart. The revised account keeps the cost meters and elapsed intervals separate.

In the post's billing description, AgentCore does not charge CPU while waiting on model or tool responses, but memory charges remain. A synchronous Lambda caller can still be billed while its connection is blocked. Releasing that caller addresses its idle compute; it does not remove the agent's inference work or every service charge.

The piece is practically significant for any team running agentic pipelines in production: the orchestration pattern determines whether you pay for a few seconds of dispatch or for the entire agent reasoning cycle.

The Shared Pipeline and Agent

The examples share a logical five-stage document flow: simulated extraction, classification, routing on shouldOrganize and shouldValidate, parallel organization and validation, then result handling. The Step Functions examples use that state machine; the durable-function version expresses orchestration in code. Describing every version as the same Step Functions execution would hide that distinction.

The agent inspects each invocation for two signals: a Step Functions task token and a durable-function callback ID. If it finds a task token, its conclude_validation tool calls sfn.send_task_success(taskToken=task_token, output=json.dumps(verdict)). If it finds a callback ID, it calls lambda_client.send_durable_execution_callback_success(CallbackId=callback_id, ...). If it finds neither, it returns the verdict inline. The same deployed agent serves all four invocation modes without modification or redeployment.

The entrypoint uses an @app.async_task decorated coroutine for background work and an @app.entrypoint handler that checks for taskToken or callbackId in the event: if either is present, it fires asyncio.create_task(validate_document_async(...)) and returns {"status": "accepted"} immediately; otherwise it awaits the agent inline.

The Three Async Patterns

Pattern 1: Task-token callback. The state uses arn:aws:states:::lambda:invoke.waitForTaskToken, passing $$.Task.Token to a dispatcher. The dispatcher invokes AgentCore with InvokeAgentRuntimeCommand and the token, then returns { dispatched: true }; Step Functions waits for SendTaskSuccess. The source state includes TimeoutSeconds: 120 and HeartbeatSeconds: 60. Heartbeat settings need to match an implementation that actually emits heartbeats, rather than being copied as a universal timeout recipe.

In one reported run, the state was active for 19.6 seconds and the dispatcher was billed for 4.8 seconds, leaving approximately 14.8 seconds without a running dispatcher. The displayed TaskSubmitted and TaskSucceeded timestamps are 14:08:19 and 14:08:34; they describe the callback gap, not the full 19.6-second state activity. The rounded readings are an illustration, not a new benchmark or a measured whole-pipeline saving.

Pattern 2: Direct service integration. The source's ASL excerpt calls arn:aws:states:::aws-sdk:bedrockagentcore:invokeAgentRuntime, with AgentRuntimeArn, RuntimeSessionId derived from States.Hash($$.Execution.Id, 'SHA-256') and a Payload constructed with States.JsonToString. It uses a ResultSelector for $.Response and TimeoutSeconds: 120. Removing the dispatcher removes Lambda from this path; a Standard workflow still has state-transition charges alongside agent and inference charges. Check the intended service integration and Region before treating an excerpt as a complete deployment recipe.

There is a source distinction to resolve before implementation: the blog also describes optimized AgentCore Harness integration, but the preceding SDK-style excerpt invokes AgentRuntime. The optimized integration documentation specifies InvokeHarness and Request Response only, excluding callback and .sync patterns. These APIs and resource definitions should not be silently substituted for each other. This review has not deployed either path.

Pattern 3: Lambda durable function. With @aws/durable-execution-sdk-js, stages use context.step and context.parallel, and the wait uses ctx.waitForCallback("validate-agentcore", async (callbackId) => dispatchAgentCore(callbackId, ...), { timeout: { seconds: 120 } }). The function suspends during that callback wait and resumes through SendDurableExecutionCallbackSuccess. The example avoids compute billing while suspended; durable execution storage and other applicable charges remain.

Pattern Comparison

Dimension Blocking (anti-pattern) Pattern 1: Task-token Pattern 2: Direct integration Pattern 3: Durable function
Lambda compute behavior, excluding other service charges Full agent processing time billed Seconds (dispatch only; 4.8 s in test run) No Lambda in this path Seconds (dispatch only)
Lambda in path Yes, alive and billed throughout Yes, returns early None Durable function (suspends)
Custom code around agent call Yes Yes Limited to ASL intrinsic functions Yes
Decouples caller from agent No Yes No Yes
Integration effort Lowest Medium-high (IAM, heartbeat, timeout) Low (single Task state) Medium (checkpoint-and-replay model)
Best for Prototypes, short agents Custom pre/post-processing logic Pure orchestration, no custom code Complex async workflows in one function

Operational Guardrails

The post names four production practices that apply across all patterns. Set TimeoutSeconds on every waitForTaskToken state so a silent agent triggers States.Timeout rather than an indefinitely paused execution; pair it with HeartbeatSeconds when the agent emits heartbeats. Derive sessionId from the Step Functions execution name so retries resume the same agent session. Enable Tracing: Active on both Step Functions and Lambda configurations so X-Ray can confirm the gap between dispatch and resume contains no running Lambda. Size the dispatcher for speed: the authors recommend 256 MB of memory and a 30-second timeout, since its only job is serializing a request and invoking the endpoint.

The authors' examples change orchestration overhead, not model quality or the correctness of a document verdict. A proposed test should include a missing callback, duplicate completion and a retry after the agent has already acted. Retain caller billing and all remaining charges separately. Our pipeline comparison explains why avoiding one idle meter is different from reducing end-to-end task latency or proving a universal architecture advantage.

Frequently asked questions

Was this a production document-validation deployment?

The AWS post uses a deliberately synthetic financing-document pipeline with simulated extraction. It illustrates invocation patterns, not a production validation result.

Is the reported 4.8-second Lambda duration a general benchmark?

No. It is the billed dispatcher duration in one illustrative run, compared with 19.6 seconds of state activity. The authors say to measure the intended workload.

Does avoiding idle Lambda compute remove the agent's charges?

No. AgentCore, model inference and applicable orchestration charges remain. The patterns change the caller's waiting cost.

Are InvokeHarness and InvokeAgentRuntime interchangeable integration examples?

No. The blog discusses optimized InvokeHarness integration while its ASL excerpt calls InvokeAgentRuntime through an SDK resource. The optimized documentation covers InvokeHarness Request Response, not task-token callbacks. Check the exact API and pattern.

Free interactive tools for the decisions this piece raises.

Related Reading