Reducing token costs and latency in Amazon Bedrock AgentCore JSON workloads

AWS published a technical walkthrough of a production agent built for Flightscape, an airline operations platform, describing three composable optimizations that together reduced token consumption by up to 71% and cut average latency by up to 80% in benchmark runs on a representative 23 MB JSON payload.
The agent is Flightscape's Training Schedule Optimization agent, which verifies crew training schedule assignments against business rules. It exposes more than 40 tools and answers conversational queries about crew assignments, scheduling and resource allocation. It is built with LangGraph, LangChain's open source graph-orchestration framework, runs on Amazon Bedrock AgentCore, and uses AgentCore memory to persist chat interactions across turns. It ingests three JSON documents: an optimization request from the Optimizer, the corresponding response, and a validation report produced by a Business Rules Validator. In its original form, average request latency exceeded 15 seconds on most calls, and every tool invocation pushed the full payload into the model context, inflating token counts and, on larger datasets, exhausting the context window before reasoning finished.
The first strategy addresses how session state is persisted. Heavy artifacts, including request JSON, response JSON, validation data and other large derived payloads, are written to Amazon S3, while AgentCore memory stores only stable references to them. A dedicated PersistedState schema holds serialized messages plus reference keys for the request, responses, cache data, validation report and basic statistics, while the live graph state retains parsed domain objects so tools can act. At the start of each turn a hydrate_state function rebuilds the live state from memory and re-parses the raw payloads; at the end of a turn, dehydrate_state strips the heavy fields and returns the lightweight form along with the artifacts to upload.
References are content-addressed rather than random: the payload is serialized with sorted keys and compact separators and the SHA-256 digest of those bytes becomes the key. This makes writes idempotent, so a turn that re-serializes an unchanged request reuses the object already in the bucket, and a small in-process cache in the S3ArtifactStore wrapper avoids a second round trip for payloads already read or written during the same invocation. The bucket and prefix come from environment variables (SESSION_STATE_BUCKET and ARTIFACT_STORE_PREFIX, the latter defaulting to artifacts).
The second strategy attacks the token cost of tool responses. Once persistence was offloaded, tool-call outputs became the dominant context consumer, with a single response in some cases exceeding 80,000 tokens, and the cost compounded as conversations continued. Tools now return a compact envelope for oversized results: a flag marking the result as oversized, a handle pointing to the full list in S3, the total count, and a small sample. When the model needs more, it calls a paging tool with the handle plus offset and limit (default 50) and receives only the requested slice along with the total, with a separate path for extracting a field from a stored dictionary.
The third strategy handles oversized dictionary outputs, which are deeply nested rather than list-shaped. Instead of returning the full dictionary, the agent returns a descriptor built by a function that walks the top-level keys (skipping those prefixed with an underscore), representing list values as a type, count and five-item sample, nested dictionaries as a type and key count, and scalars inline with their value. If the serialized tool content still exceeds a character limit, the full payload is stored and the response replaced by its descriptor. The result stays navigable for the model while sharply reducing tokens, and deeper detail is fetched through the same handle mechanism.
The published results come from running an identical set of questions over the same dataset against pre- and post-optimization versions, compared in Amazon Bedrock AgentCore Observability. Over a five-question conversation, total token usage fell from roughly 289,500 to about 82,900, around 71%, and average trace latency dropped from 15.4 seconds to 3.1 seconds, nearly 80%. Across 100 sample questions, total tokens fell from 23.6 million to 8.5 million (64%) and average trace latency from 7,930 ms to 2,461 ms (69%). The largest gains were on heavy single-turn payloads, such as a query listing valid crews across every validation category.
The prerequisites are an AWS account with Amazon Bedrock access and a supported model, an S3 bucket for artifacts, AgentCore with AgentCore memory enabled, a LangGraph-style orchestrator with tool calling, and Python 3.12+ with boto3, langchain-core and langgraph. Companion code is linked in a GitHub repository, and Amazon Bedrock Guardrails can be layered on to filter conversational responses.
A likely trade-off is added moving parts: each artifact now costs an S3 write and a later read, so the approach pays off when payloads are large relative to the model call but adds complexity and storage for small ones. Content addressing also means payloads are effectively immutable, which suits read-heavy audit and validation data but requires care when state must be edited in place.
Why it matters: Teams building agents over large structured documents, such as scheduling, claims, ERP or log data, are most affected, since their cost and latency are driven by repeated payload injection rather than model choice. The pattern, keeping bulk data in object storage and handing the model handles plus summaries, is implementable outside AWS with any store that supports stable keys. The reported figures suggest that context management, not just model selection, can be the largest lever on agent economics, though the benchmarks cover a single workload and may not transfer directly.
Based on reporting from the original publisher. Visit the source for full context and later updates.
Publisher excerpt
As production agents on Amazon Bedrock AgentCore process multi-megabyte JSON payloads, token costs and latency climb. This post shares three composable strategies: offloading heavy artifacts to Amazon S3, on-demand result paging, and dictionary compaction. Together they cut per-turn token use by up to 71% and latency by up to 80%.