LangChain AWS

repository·main·Indexed 43 days ago

https://github.com/langchain-ai/langchain-aws

A collection of LangChain and LangGraph components for Amazon Web Services (AWS). Includes integrations for Amazon Bedrock AgentCore Code Interpreter via AgentCoreSandbox, long-term memory with AgentCoreMemoryStore, state persistence using AgentCoreMemorySaver and DynamoDBSaver, and support for ChatBedrockConverse.

Tokens
56.6K
Snippets
175
Records
215
Agent score
56%

What's inside LangChain AWS

  1. How automatic cleanup works with TTL and S3

    main

    The DynamoDBSaver provides a unified cleanup mechanism using DynamoDB TTL and S3 Lifecycle policies.

    • DynamoDB TTL: When you set ttl_seconds, each checkpoint is tagged with an expiration time. DynamoDB deletes expired items automatically without consuming write throughput (though deletion may take a few days).
    • S3 Lifecycle: If S3 offloading is enabled, the library automatically configures S3 lifecycle rules based on your ttl_seconds. It converts seconds to days to match S3's day-level precision.

    Setting ttl_seconds synchronizes cleanup across both layers, so you do not need to manually configure S3 lifecycle policies.

  2. Configure Valkey vector search with ValkeyIndexConfig

    main

    When using ValkeyStore, you can enable enhanced vector search by providing a ValkeyIndexConfig. This supports two algorithms:

    • HNSW (Hierarchical Navigable Small World): Best for large datasets (>10K vectors). Offers fast approximate search with configurable speed/accuracy trade-offs.
    • FLAT (Brute Force): Best for small datasets (<10K vectors). Provides exact search with perfect accuracy but slower performance on large datasets.

    HNSW Tuning Parameters:

    • hnsw_m: Connections per layer (4-64, default 16). Higher = better recall, more memory.
    • hnsw_ef_construction: Construction search width (100-800, default 200). Higher = better index quality, slower construction.
    • hnsw_ef_runtime: Query search width (10-500, default 10). Higher = better recall, slower queries.
    from langgraph_checkpoint_aws.store.valkey import ValkeyIndexConfig
    
    index_config = {
        "collection_name": "my_documents",
        "dims": 1536,
        "embed": embeddings,
        "fields": ["text", "content"],
        "timezone": "UTC",
        "index_type": "hnsw",  # "hnsw" or "flat"
        "hnsw_m": 16,
        "hnsw_ef_construction": 200,
        "hnsw_ef_runtime": 10,
    }
  3. AgentCoreValkeySaver Key Generation Pattern

    main

    The AgentCoreValkeySaver uses a structured key naming convention for its storage in Valkey. The session_id is derived from thread_id and checkpoint_ns (joined by an underscore if a namespace is present).

    Key Patterns:

    • agentcore:session:{session_id}:{actor_id}:checkpoints — Session checkpoint list
    • agentcore:checkpoint:{session_id}:{actor_id}:{ns}:{cp_id} — Individual checkpoint
    • agentcore:writes:{session_id}:{actor_id}:{ns}:{cp_id} — Pending writes
    • agentcore:channel:{session_id}:{actor_id}:{ns}:{ch}:{cp_id} — Channel data

    Example Keys:

    • agentcore:session:user-123_prod:assistant-agent:checkpoints
    • agentcore:checkpoint:user-123_prod:assistant-agent:prod:cp_001
  4. Compare DynamoDB Store vs Valkey Store

    main

    When choosing a storage backend for LangGraph, consider the following trade-offs:

    • DynamoDB Store: Best for cost-effective, fully managed persistent storage. It allows you to serve both key-value memory and semantic search from a single table without needing a separate vector database.
    • Valkey Store: Best for ultra-low latency and high-performance requirements.
    FeatureDynamoDB StoreValkey Store
    Vector SearchYes (DynamoDB vector index)Yes
    PerformanceGoodExcellent
    Async SupportYes (thread pool)Yes
    Best ForCost-effective persistent storageVector search, ultra-high performance
  5. Understand AgentCore sandbox session behavior

    main

    AgentCore sessions are tied to the lifecycle of the CodeInterpreter.

    • Isolation: Each call to interpreter.start() creates a fresh, isolated MicroVM.
    • Persistence: Sessions cannot be reconnected once interpreter.stop() is called.
    • Expiration: Sessions auto-expire after a configurable timeout (default is 15 minutes, with a maximum of 8 hours).
  6. Compare AgentCoreValkeySaver, ValkeySaver, and AgentCoreMemory

    main

    Choose the right saver based on your integration needs:

    • AgentCoreValkeySaver: Best for AWS Bedrock AgentCore projects, multi-agent systems requiring actor routing, and production systems needing retry logic. It is self-hosted.
    • ValkeySaver: Best for standard LangGraph applications and simple single-agent use cases. It does not support AgentCore integration or actor management.
    • AgentCoreMemory: A fully managed AWS solution with zero infrastructure management, ideal for enterprise-scale deployments.
  7. Choose between AgentCoreValkeySaver and ValkeySaver

    main

    This package provides two distinct Valkey-based checkpoint savers. Crucially, they use incompatible storage formats; data stored by one cannot be read by the other.

    AgentCoreValkeySaver

    Use this when:

    • Integrating with AWS Bedrock AgentCore Memory services.
    • Building multi-agent systems requiring actor_id for session organization.
    • You need built-in exponential backoff retry logic and Pydantic validation.
    • Import Path: langgraph_checkpoint_aws.checkpoint.agentcore.valkey

    ValkeySaver

    Use this when:

    • Building standard LangGraph applications with simple thread-based checkpointing.
    • You want minimal dependencies and a simpler key structure.
    • You do not plan to use AgentCore services.
    • Import Path: langgraph_checkpoint_aws.checkpoint.valkey
  8. Enable Semantic Search with Vector Index

    main

    You can enable semantic search by providing an index configuration in the DynamoDBStore constructor. This uses a DynamoDB vector index to perform approximate nearest neighbor search without a separate vector database.

    Note: The vector index is eventually consistent; items written immediately before a search might not appear in results. If no index is provided, search() performs standard namespace listing and filtering.

    from langgraph_checkpoint_aws import DynamoDBStore
    from langchain_aws import BedrockEmbeddings
    
    store = DynamoDBStore(
        table_name="agent-ltm",
        index={
            "embed": BedrockEmbeddings(model_id="amazon.titan-embed-text-v2:0"),
            "dims": 256,  # must match the embedding model's output
            "fields": ["text"],  # which value fields to embed
        },
    )
    store.setup()
    
    # Embedding is computed automatically on put()
    store.put(("user", "alice"), "mem1", {"text": "Alice prefers dark mode"})
    
    # Semantic search returns results ranked by relevance
    results = store.search(("user", "alice"), query="UI preferences")
    for item in results:
        print(item.score, item.value)
  9. Setup Valkey for LangGraph checkpointing

    main

    You can use Valkey (a Redis-compatible storage) as a checkpointer.

    Connect to an ElastiCache cluster within your VPC:

    from langgraph_checkpoint_aws.checkpoint.valkey import ValkeySaver
    
    with ValkeySaver.from_conn_string(
        "valkeys://your-elasticache-cluster.amazonaws.com:6379", pool_size=20
    ) as checkpointer:
        pass

    Local Docker Setup

    To run Valkey locally for development:

    # Start Valkey with required modules
    docker run --name valkey-bundle -p 6379:6379 -d valkey/valkey-bundle:latest
    
    # Or with custom configuration
    docker run --name valkey-custom \
      -p 6379:6379 \
      -v $(pwd)/valkey.conf:/etc/valkey/valkey.conf \
      -d valkey/valkey-bundle:latest
  10. Run tests using the Makefile

    main

    To ensure consistent test results and prevent AWS credentials from leaking into serialization snapshots, always use the make command instead of running pytest directly. The Makefile automatically unsets sensitive AWS environment variables (such as AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN, BEDROCK_AWS_ACCESS_KEY_ID, and BEDROCK_AWS_SECRET_ACCESS_KEY) during the test execution.

    # Run all unit tests
    make test
    
    # Run specific test file
    make test TEST_FILE=tests/unit_tests/test_example.py
    
    # Run specific test case
    make test TEST_FILE=tests/unit_tests/test_example.py::TestClass::test_method
  11. Use Async operations with LangGraph Checkpoint AWS

    main

    All components in langgraph-checkpoint-aws support asynchronous operations. You can use AsyncBedrockSessionSaver, AsyncValkeySaver, and DynamoDBSaver (via ainvoke) for non-blocking workflows.

    Common async patterns include:

    • Creating Bedrock sessions using AsyncBedrockSessionSaver.
    • Running LangGraph with DynamoDBSaver using ainvoke.
    • Using AsyncValkeySaver within an async with context manager.
    • Using AsyncValkeyStore for key-value operations with aput and aget.
    from langgraph_checkpoint_aws.async_saver import AsyncBedrockSessionSaver
    from langgraph_checkpoint_aws import AsyncValkeySaver, DynamoDBSaver, AsyncValkeyStore
    
    # Async Bedrock usage
    session_saver = AsyncBedrockSessionSaver(region_name="us-west-2")
    session_id = (await session_saver.session_client.create_session()).session_id
    
    # Async DynamoDB usage
    checkpointer = DynamoDBSaver(table_name="my-checkpoints", region_name="us-west-2")
    result = await graph.ainvoke(1, {"configurable": {"thread_id": "session-1"}})
    
    # Async ValkeySaver usage
    async with AsyncValkeySaver.from_conn_string("valkey://localhost:6379") as checkpointer:
        graph = builder.compile(checkpointer=checkpointer)
        result = await graph.ainvoke(1, {"configurable": {"thread_id": "session-1"}})
    
    # Async ValkeyStore usage
    async with AsyncValkeyStore.from_conn_string("valkey://localhost:6379") as store:
        namespace = ("example",)
        key = "key"
        data = {"message": "Sample message"}
        await store.setup()
        await store.aput(namespace, key, data)
        result = await store.aget(namespace, key)
  12. Use AsyncBedrockSessionSaver for asynchronous LangGraph execution

    main

    For asynchronous workflows, use AsyncBedrockSessionSaver. This requires awaiting the session creation and using ainvoke on the compiled graph.

    from langgraph.graph import StateGraph
    from langgraph_checkpoint_aws.async_saver import AsyncBedrockSessionSaver
    
    # Initialize the saver
    session_saver = AsyncBedrockSessionSaver(
        region_name="us-west-2",  # Your AWS region
        credentials_profile_name="default",  # Optional: AWS credentials profile
    )
    
    # Create a session
    session_create_response = await session_saver.session_client.create_session()
    session_id = session_create_response.session_id
    
    # Use with LangGraph
    builder = StateGraph(int)
    builder.add_node("add_one", lambda x: x + 1)
    builder.set_entry_point("add_one")
    builder.set_finish_point("add_one")
    
    graph = builder.compile(checkpointer=session_saver)
    config = {"configurable": {"thread_id": session_id}}
    await graph.ainvoke(1, config)