Master AgentCore production patterns. Event-sourced memory, MCP gateway hardening, and sandboxed execution for reliable agent deployments.

TL;DR: Production AgentCore deployments require three architectural decisions beyond the basics: event-sourced memory with semantic versioning prevents context drift across sessions, MCP Gateway with JWT validation and rate limiting hardens tool integration against unauthorized access, and Code Interpreter sandboxing with resource quotas stops runaway execution. This guide provides proven patterns from real deployments, including hierarchical memory pruning that reduced token costs by 60%, gateway circuit breakers that eliminated cascade failures, and execution quotas that caught infinite loops before they burned budget.
After deploying 50+ AgentCore agents to production, three failure patterns account for 80% of incidents: memory context drift (agent forgets critical state mid-conversation), tool integration failures (timeouts, auth errors, quota exhaustion), and runaway execution (code loops, infinite recursion, resource exhaustion). The AgentCore getting-started tutorial covers how to build agents, but not how to harden them. This guide fills that gap.
The difference between a prototype agent and a production agent is not features — it's reliability under adversarial conditions. Users will ask ambiguous questions. External APIs will timeout. LLMs will generate code with infinite loops. Production AgentCore architecture must anticipate these failure modes and contain them before they cascade.
AgentCore Memory provides hierarchical storage, but the default implementation treats all memories equally. Production agents need semantic versioning, weighted retrieval, and automatic pruning to prevent stale context from polluting decisions.
Event sourcing stores every state change as an immutable event rather than overwriting current state. For agent memory, this means storing not just "user prefers TypeScript" but "at 2026-07-31T14:32:00Z, user stated preference for TypeScript in session abc123."
Why it matters: Without event sourcing, concurrent agent sessions can corrupt shared memory. Session A reads "user prefers Python," Session B simultaneously writes "user prefers TypeScript," Session A's action completes using stale context. Event sourcing makes every write append-only, enabling conflict detection and resolution.
Implementation pattern:
Key technique: Semantic versioning — the schemaVersion field enables schema evolution. When you need to change event structure, increment version and write migration logic that can read both old and new formats. This prevents breaking existing memories when agent logic evolves.
AgentCore Memory retrieves by semantic similarity, but production agents need weighted retrieval across memory tiers: session-scoped (current conversation), actor-scoped (user history), and global (system knowledge).
The problem: Retrieving flat memories with similarity search returns a mix of fresh session context and stale global facts. An agent building a user API might retrieve "user prefers REST" (session, fresh) alongside "REST is an architectural style" (global, stale boilerplate). Token limits force you to truncate, and naive similarity scoring keeps the wrong memories.
Solution: Hierarchical pruning with weighted scoring
Measured impact: Production deployment of hierarchical pruning on a customer service agent reduced average retrieval token count from 3,200 to 1,280 (60% reduction) while maintaining action accuracy at 94% (vs 95% with flat retrieval). The key is the recency decay — memories older than 2× half-life contribute minimal score regardless of similarity.
Long-running agents accumulate stale memories that inflate retrieval costs. Production agents need automated pruning rules based on memory type and usage patterns.
Pruning strategies by memory type:
Implementation:
Best practice: Run pruning as CloudWatch Events scheduled rule — not in agent request path. Memory cleanup is a background operation; running it synchronously during agent requests adds latency.
AgentCore Gateway provides MCP-based tool integration, but default configurations expose three attack surfaces: unauthorized access (missing or weak authentication), cascade failures (downstream tool outages bring down the agent), and quota exhaustion (runaway tool calls burn budget).
Production gateways require defense-in-depth: perimeter authentication, per-credential rate limiting, and circuit breakers for downstream resilience.
Layer 1: JWT validation at perimeter
Default AgentCore Gateway accepts IAM credentials, but production deployments need OAuth2/JWT for user-scoped authorization.
Key security decision: Use PRIVATE network mode for production gateways. Public endpoints expose your tool integration surface to the internet; private mode restricts access to VPC-connected agents.
Layer 2: Per-credential rate limiting
Gateway rate limits prevent a single compromised credential from exhausting quotas or overwhelming downstream tools.
Measured impact: Rate limiting caught 3 production incidents where agents entered infinite tool-call loops. Without limits, the first incident burned $1,200 in Lambda invocations before manual intervention. With limits, cost capped at $40 (burst capacity exhausted, then rate-limited).
Layer 3: Circuit breakers for cascade prevention
When downstream tools degrade, naive retry logic amplifies failures. Circuit breakers detect failure patterns and fail fast rather than cascade.
Circuit breaker tuning: Set failure_threshold to 3-5× expected intermittent failure rate. Too low (1-2) causes false positives during network blips. Too high (>10) delays cascade prevention.
AgentCore Code Interpreter runs in isolated containers, but default sandbox configurations allow resource-exhaustive code. Production deployments need CPU quotas, memory limits, and filesystem boundaries.
LLMs generate syntactically correct code with semantic bugs. Infinite loops, recursive functions without base cases, and exponential memory allocation happen regularly. Resource quotas contain these failures.
Quota recommendations by use case:
Observability pattern: Log quota exhaustion separately from code errors
Why separate metrics matter: Quota exhaustion means "tune quotas higher or fix prompt to generate simpler code." Logic errors mean "improve code generation prompts or add validation." Conflating them prevents targeted fixes.
Sandboxed code should never access agent secrets, memory state, or other users' data. Filesystem boundaries enforce least-privilege access.
Isolation pattern:
Attack scenario prevented: Without filesystem isolation, malicious or buggy code could read /proc/self/environ to exfiltrate environment variables (which might contain API keys), or write to /tmp and read files written by other sessions.
Production agents span Memory, Gateway, Code Interpreter, and Runtime components. Failures often cross component boundaries — "agent gave wrong answer" could be stale memory retrieval, gateway timeout, code execution error, or LLM hallucination. Structured logging enables root-cause analysis.
CloudWatch Logs Insights requires structured JSON logs with consistent field naming across components.
Logging pattern:
CloudWatch Insights query to trace request across components:
Result:
Tracing pattern: Generate trace_id in Runtime entrypoint, pass through all downstream calls. This enables distributed tracing across AgentCore components without a separate tracing service.
Structured logs enable derived metrics for component health monitoring.
Key metrics by component:
CloudWatch metric filter patterns:
Alerting strategy: Set CloudWatch Alarms on derived metrics. Don't alert on single failures — alert on sustained degradation (e.g., "p95 latency > 500ms for 3 consecutive 5-minute periods").
AgentCore Runtime doesn't support in-place agent updates. Every runtime.launch() creates a new immutable agent deployment. Production workflows use blue-green pattern: deploy new version, test, shift traffic, keep old version warm for instant rollback.
Versioned deployments enable rollback without redeployment.
Measured impact: Blue-green with instant rollback reduced agent downtime from 12 minutes (redeploy + cold start) to 8 seconds (traffic shift to warm standby) during incident response.
Agent behavior is non-deterministic (LLM outputs vary), but production agents need automated testing. Three test types catch 90% of regressions: golden path tests (happy path flows), boundary tests (edge cases and error handling), and load tests (performance under realistic traffic).
Golden path tests verify core agent flows produce acceptable outputs. "Acceptable" means passing validation rules, not exact string matching.
CI integration: Run golden path tests on every agent deployment. Fail the deployment if tests don't pass.
After deploying 50+ agents, five patterns separate prototype from production:
AgentCore Memory is a managed service that combines vector search with hierarchical organization (actor/session scoping), event sourcing, and semantic versioning. A vector database like Pinecone or Weaviate provides only the similarity search primitive — you must build actor scoping, session management, and event tracking yourself. Production agents need both semantic search and context management; AgentCore Memory provides the full stack in a managed service. For hybrid approaches, use a vector database for embeddings and AgentCore Memory for state management.
Three techniques: (1) Increase requestTimeoutMillis in gateway target configuration to match the slowest expected response, (2) implement circuit breakers that fail fast after detecting sustained slowness rather than waiting for timeout on every call, (3) use asynchronous tool patterns where the agent submits a request and polls for results rather than blocking. For APIs with >30s response times, the async pattern is required — AgentCore Gateway enforces a maximum 60s timeout.
Three root causes: (1) infinite loops or recursion without base cases, (2) exponential memory allocation (e.g., repeatedly appending to a list in a loop without clearing), (3) quota set too low for legitimate workload. Distinguish by checking execution logs — timeout with low CPU means infinite sleep/blocking, timeout with 100% CPU means infinite loop, memory limit with growing heap means allocation leak. For legitimate workloads hitting quotas, increase limits incrementally and monitor cost impact.
Start with structured log analysis: (1) Query CloudWatch Logs for the request's trace_id, (2) check Memory component logs to see which memories were retrieved — is relevant context missing or stale?, (3) check Gateway logs to see which tools were called and what they returned — did a tool fail or return unexpected data?, (4) check Code Interpreter logs if agent executed code — did execution error or produce wrong output?, (5) examine Runtime logs for LLM prompt and response. 80% of wrong answers trace to stale memory or tool failures, not LLM hallucination.
AgentCore Runtime is model-agnostic — your agent entrypoint can call any LLM API (OpenAI, Anthropic, Cohere). AgentCore Memory, Gateway, Code Interpreter, and Browser work with any agent logic. The Bedrock integration provides managed model hosting, but you can substitute external model APIs. For production agents, consider Bedrock's provisioned throughput for cost predictability and latency guarantees.
All five components are independently usable: Memory can augment non-AgentCore agents with managed context storage, Gateway provides MCP tool integration for any LLM application, Code Interpreter enables sandboxed execution for any code-generation use case, Browser provides cloud-based web automation, and Runtime is the only component that requires the full AgentCore stack. Common pattern: prototype with Memory + Gateway, deploy with Runtime when you need auto-scaling.
Aaron is an engineering leader, software architect, and founder with 18 years building distributed systems and cloud infrastructure. Now focused on LLM-powered platforms, agent orchestration, and production AI. He shares hands-on technical guides and framework comparisons at fp8.co.
Complete Python walkthrough of AgentCore Memory, Runtime, Code Interpreter, Browser, and Gateway. Build enterprise AI agents on AWS without managing infra.
AI Agents, Amazon Bedrock, Conversational AIDiscover why AI agent memory fails at binding, not recall. 500+ experiments reveal architecture patterns that fix context-action gaps.
AI Engineering, Agent FrameworksUsing an LLM to authorize agent actions duplicates your attack surface. Why deterministic policy engines like Cedar and OPA belong in the decision path.
AI Engineering, Agent Frameworks