All Frameworks

Amazon Bedrock Agents Quickstart

Give your Bedrock Agents persistent memory via a Lambda action group backed by AgenticMemory.

1Install in a Lambda Layer

Create a Lambda layer with the Python SDK so your action group handler can import it.

# Build the layer zip
mkdir -p python
pip install agenticmemory-sdk -t python/
zip -r agenticmemory-layer.zip python/

# Upload via AWS CLI
aws lambda publish-layer-version \
  --layer-name agenticmemory-sdk \
  --zip-file fileb://agenticmemory-layer.zip \
  --compatible-runtimes python3.11 python3.12

Set the AGENTICMEMORY_API_KEY environment variable on your Lambda function.

aws lambda update-function-configuration \
  --function-name my-bedrock-agent \
  --environment Variables={AGENTICMEMORY_API_KEY=amk_your_key_here}

2Lambda Action Group Handler

Write a Lambda that stores interactions, recalls context, and searches past sessions for your Bedrock Agent.

import json
from agenticmemoryai import AgenticMemory

memory = AgenticMemory()
SPACE_ID = "your_space_id"

def lambda_handler(event, context):
    agent = event["agent"]
    action_group = event["actionGroup"]
    api_path = event["apiPath"]
    parameters = event.get("parameters", [])
    body = event.get("requestBody", {}).get("content", {})

    # Recall recent context before every invocation
    history = memory.recall(SPACE_ID, limit=10)
    prior = [m["content"] for m in history.get("messages", [])]

    if api_path == "/store":
        # Store the agent's interaction
        content = _get_param(parameters, "content")
        role = _get_param(parameters, "role") or "assistant"
        memory.store(SPACE_ID, role=role, content=content)
        response_body = {"status": "stored", "priorMessages": len(prior)}

    elif api_path == "/recall":
        # Return recent conversation history
        limit = int(_get_param(parameters, "limit") or 10)
        result = memory.recall(SPACE_ID, limit=limit)
        response_body = {"messages": result.get("messages", [])}

    elif api_path == "/search":
        # Search for relevant past sessions
        query = _get_param(parameters, "query")
        results = memory.search(SPACE_ID, query, scope="all", tags=[])
        response_body = {"results": results.get("results", [])}

    else:
        response_body = {"error": f"Unknown path: {api_path}"}

    return {
        "messageVersion": "1.0",
        "response": {
            "actionGroup": action_group,
            "apiPath": api_path,
            "httpMethod": event.get("httpMethod", "POST"),
            "httpStatusCode": 200,
            "responseBody": {
                "application/json": {
                    "body": json.dumps(response_body)
                }
            }
        }
    }

def _get_param(params, name):
    for p in params:
        if p["name"] == name:
            return p["value"]
    return None

3Deploy & Test

Attach the Lambda as an action group in the Bedrock console and invoke your agent.

# Test the Lambda directly
aws lambda invoke \
  --function-name my-bedrock-agent \
  --payload '{
    "agent": "test",
    "actionGroup": "MemoryActions",
    "apiPath": "/store",
    "httpMethod": "POST",
    "parameters": [
      {"name": "content", "value": "User prefers concise answers"},
      {"name": "role", "value": "system"}
    ]
  }' \
  response.json

cat response.json
# {"messageVersion":"1.0","response":{...,"httpStatusCode":200,...}}

# Verify it was stored
aws lambda invoke \
  --function-name my-bedrock-agent \
  --payload '{
    "agent": "test",
    "actionGroup": "MemoryActions",
    "apiPath": "/recall",
    "parameters": [{"name": "limit", "value": "5"}]
  }' \
  recall.json

cat recall.json
# {"messageVersion":"1.0","response":{...,"messages":[{"role":"system","content":"User prefers concise answers"},...]}}

Free tier: 1 space, 1K messages/month. Upgrade to Pro ($24.99/mo) for semantic search + auto-summaries.

Help
Help

Click the icon on any page for context-sensitive help.

Quick Links