LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
    • Overview
    • Caches
    • Callbacks
    • Documents
    • Document loaders
    • Embeddings
    • Exceptions
    • Language models
    • Serialization
    • Output parsers
    • Prompts
    • Rate limiters
    • Retrievers
    • Runnables
    • Utilities
    • Vector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    OverviewCachesCallbacksDocumentsDocument loadersEmbeddingsExceptionsLanguage modelsSerializationOutput parsersPromptsRate limitersRetrieversRunnablesUtilitiesVector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    Language
    Theme
    Pythonlangchain-corecachesInMemoryCache
    Classā—Since v0.1

    InMemoryCache

    Cache that stores things in memory.

    Copy
    InMemoryCache(
        self,
        *,
        maxsize: int | None = None,
    )

    Bases

    BaseCache

    Used in Docs

    • Graph API overview
    • Use the functional API
    • Use the graph API

    Constructors

    Methods

    View source on GitHub

    Parameters

    NameTypeDescription
    maxsizeint | None
    Default:None
    constructor
    __init__
    NameType
    maxsizeint | None
    method
    lookup
    method
    update
    method
    clear
    method
    alookup
    method
    aupdate
    method
    aclear

    Example:

    from langchain_core.caches import InMemoryCache
    from langchain_core.outputs import Generation
    
    # Initialize cache
    cache = InMemoryCache()
    
    # Update cache
    cache.update(
        prompt="What is the capital of France?",
        llm_string="model='gpt-5.4-mini',
        return_val=[Generation(text="Paris")],
    )
    
    # Lookup cache
    result = cache.lookup(
        prompt="What is the capital of France?",
        llm_string="model='gpt-5.4-mini',
    )
    # result is [Generation(text="Paris")]

    The maximum number of items to store in the cache.

    If None, the cache has no maximum size.

    If the cache exceeds the maximum size, the oldest items are removed.

    Look up based on prompt and llm_string.

    Update cache based on prompt and llm_string.

    Clear cache.

    Async look up based on prompt and llm_string.

    Async update cache based on prompt and llm_string.

    Async clear cache.