AI Agents, Amazon Bedrock, Production AI22 min read

AgentCore Best Practices: Memory, Tooling, Security

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

AgentCore Best Practices: Memory, Tooling, Security

How To Get More From Amazon Bedrock AgentCore: Best Practices For Memory, Tooling, And Security

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.

Key Takeaways

  • Event-sourced memory with semantic versioning enables time-travel debugging and prevents context corruption when multiple agent sessions modify shared state concurrently.
  • Hierarchical memory pruning (session → actor → global) reduces retrieval token costs by 60% while maintaining contextual relevance through weighted scoring across memory tiers.
  • MCP Gateway security requires three layers: JWT validation at the perimeter, rate limiting per credential, and circuit breakers to prevent cascade failures when downstream tools degrade.
  • Code Interpreter sandboxing must enforce CPU quotas, memory limits, and filesystem boundaries — production agents can generate infinite loops or resource-exhaustive code without these constraints.
  • Cross-component observability through CloudWatch structured logs enables root-cause analysis when agents fail across Memory retrieval, Gateway tool calls, and Code execution boundaries.
  • Agent versioning with immutable Runtime deployments allows blue-green rollouts and instant rollback when production agents exhibit behavioral regressions after model or prompt updates.

Why do production agents fail?

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.

What memory patterns prevent context drift?

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.

What is event-sourced memory?

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.

How does hierarchical memory pruning work?

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.

What memory cleanup patterns prevent bloat?

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.

How do you harden MCP Gateway tool integration?

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).

What are the three layers of gateway security?

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.

What sandboxing prevents runaway code execution?

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.

What resource quotas prevent infinite loops?

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.

How do filesystem boundaries prevent data exfiltration?

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.

How does cross-component observability work?

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.

What structured log format enables tracing?

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.

What metrics indicate component degradation?

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").

How do blue-green deployments work with Runtime?

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.

What is the agent versioning pattern?

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.

What testing catches regressions before production?

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).

What are golden path tests?

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.

What are the 5 architecture patterns for production?

After deploying 50+ agents, five patterns separate prototype from production:

  1. Event-sourced memory with hierarchical retrieval — prevents context drift, enables time-travel debugging, reduces token costs 60%
  2. Defense-in-depth gateway security — JWT + rate limits + circuit breakers eliminate cascade failures
  3. Resource-quoted code sandboxing — CPU/memory/filesystem limits contain runaway execution
  4. Cross-component structured logging — unified trace IDs enable distributed debugging
  5. Blue-green deployments with instant rollback — versioned agents reduce incident response time from 12min to 8sec

Frequently Asked Questions

What is the difference between AgentCore Memory and a vector database?

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.

How do I prevent AgentCore Gateway from timing out on slow external APIs?

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.

What causes AgentCore Code Interpreter to fail with "quota exhausted"?

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.

How do I debug an agent that gives wrong answers?

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.

Can I use AgentCore with models other than Bedrock models?

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.

What AgentCore components can I use independently?

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.

📬 Get this weekly →

Subscribe to the newsletter

By subscribing, you agree to our Terms of Service and Privacy Policy.

About the Author

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.

Cite this Article

Aaron. "AgentCore Best Practices: Memory, Tooling, Security." fp8.co, July 31, 2026. https://fp8.co/articles/how-to-get-more-from-amazon-bedrock-agentcore-best-practices

Related Articles

AWS AgentCore Explained: 5 Tools for Production AI Agents

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 AI

AI Agent Memory: Why Binding Matters More Than Recall

Discover why AI agent memory fails at binding, not recall. 500+ experiments reveal architecture patterns that fix context-action gaps.

AI Engineering, Agent Frameworks

AI Agent Authorization: Don't Let the LLM Decide

Using 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