From c399485e3b5fb0d6b8516fe33d9222a14082b452 Mon Sep 17 00:00:00 2001 From: Theo van Kraay Date: Thu, 6 Nov 2025 12:12:35 +0000 Subject: [PATCH 1/4] add cosmos db sample for checkpointer --- src/oss/langchain/short-term-memory.mdx | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/oss/langchain/short-term-memory.mdx b/src/oss/langchain/short-term-memory.mdx index fb771648f9..254db1f1a1 100644 --- a/src/oss/langchain/short-term-memory.mdx +++ b/src/oss/langchain/short-term-memory.mdx @@ -107,6 +107,40 @@ const checkpointer = PostgresSaver.fromConnString(DB_URI); ``` ::: +You can also use Azure Cosmos DB as a checkpoint saver: + +:::python +```shell +pip install langgraph-checkpoint-cosmosdb +``` + +```python +from langchain.agents import create_agent +from langgraph_checkpoint_cosmosdb import CosmosDBSaver # [!code highlight] +import os + +# Set environment variables for authentication +os.environ["COSMOSDB_ENDPOINT"] = "your_cosmosdb_endpoint" +os.environ["COSMOSDB_KEY"] = "your_cosmosdb_key" + +# Database and Container are created if they don't exist +checkpointer = CosmosDBSaver( # [!code highlight] + database_name="your_database", # [!code highlight] + container_name="your_container" # [!code highlight] +) # [!code highlight] + +agent = create_agent( + "gpt-5", + [get_user_info], + checkpointer=checkpointer, # [!code highlight] +) +``` + + + Azure Cosmos DB checkpointer supports both sync and async operations. If the database and container already exist, you can use default RBAC credentials (e.g., `az login`) instead of setting the endpoint and key. + +::: + ## Customizing agent memory From f6daf1efb441d6cab317b7712a4168910786a8bc Mon Sep 17 00:00:00 2001 From: Theo van Kraay Date: Mon, 10 Nov 2025 21:03:33 +0000 Subject: [PATCH 2/4] Update src/oss/langchain/short-term-memory.mdx Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/oss/langchain/short-term-memory.mdx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/oss/langchain/short-term-memory.mdx b/src/oss/langchain/short-term-memory.mdx index 254db1f1a1..c03fc7ee5c 100644 --- a/src/oss/langchain/short-term-memory.mdx +++ b/src/oss/langchain/short-term-memory.mdx @@ -124,16 +124,16 @@ os.environ["COSMOSDB_ENDPOINT"] = "your_cosmosdb_endpoint" os.environ["COSMOSDB_KEY"] = "your_cosmosdb_key" # Database and Container are created if they don't exist -checkpointer = CosmosDBSaver( # [!code highlight] +with CosmosDBSaver( # [!code highlight] database_name="your_database", # [!code highlight] container_name="your_container" # [!code highlight] -) # [!code highlight] +) as checkpointer: # [!code highlight] -agent = create_agent( - "gpt-5", - [get_user_info], - checkpointer=checkpointer, # [!code highlight] -) + agent = create_agent( + "gpt-5", + [get_user_info], + checkpointer=checkpointer, # [!code highlight] + ) ``` From 712ae82313d8058d63519905755faaa94de8200c Mon Sep 17 00:00:00 2001 From: Theo van Kraay Date: Mon, 10 Nov 2025 21:03:49 +0000 Subject: [PATCH 3/4] Update src/oss/langchain/short-term-memory.mdx Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/oss/langchain/short-term-memory.mdx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/oss/langchain/short-term-memory.mdx b/src/oss/langchain/short-term-memory.mdx index c03fc7ee5c..d985ed1fe7 100644 --- a/src/oss/langchain/short-term-memory.mdx +++ b/src/oss/langchain/short-term-memory.mdx @@ -136,12 +136,11 @@ with CosmosDBSaver( # [!code highlight] ) ``` +::: + Azure Cosmos DB checkpointer supports both sync and async operations. If the database and container already exist, you can use default RBAC credentials (e.g., `az login`) instead of setting the endpoint and key. -::: - - ## Customizing agent memory By default, agents use @[`AgentState`] to manage short term memory, specifically the conversation history via a `messages` key. From 60b2350115c72bf993cd7237fa11f6c12577bf54 Mon Sep 17 00:00:00 2001 From: Theo van Kraay Date: Mon, 10 Nov 2025 21:12:32 +0000 Subject: [PATCH 4/4] Fix CosmosDBSaver usage to match actual implementation --- src/oss/langchain/short-term-memory.mdx | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/oss/langchain/short-term-memory.mdx b/src/oss/langchain/short-term-memory.mdx index d985ed1fe7..9138212245 100644 --- a/src/oss/langchain/short-term-memory.mdx +++ b/src/oss/langchain/short-term-memory.mdx @@ -115,7 +115,7 @@ pip install langgraph-checkpoint-cosmosdb ``` ```python -from langchain.agents import create_agent +from langgraph.graph import StateGraph, MessagesState, START from langgraph_checkpoint_cosmosdb import CosmosDBSaver # [!code highlight] import os @@ -124,16 +124,16 @@ os.environ["COSMOSDB_ENDPOINT"] = "your_cosmosdb_endpoint" os.environ["COSMOSDB_KEY"] = "your_cosmosdb_key" # Database and Container are created if they don't exist -with CosmosDBSaver( # [!code highlight] +checkpointer = CosmosDBSaver( # [!code highlight] database_name="your_database", # [!code highlight] container_name="your_container" # [!code highlight] -) as checkpointer: # [!code highlight] +) # [!code highlight] - agent = create_agent( - "gpt-5", - [get_user_info], - checkpointer=checkpointer, # [!code highlight] - ) +# Build your graph with the checkpointer +builder = StateGraph(MessagesState) +builder.add_node("call_model", call_model) +builder.add_edge(START, "call_model") +graph = builder.compile(checkpointer=checkpointer) # [!code highlight] ``` ::: @@ -712,7 +712,7 @@ def update_user_info( runtime: ToolRuntime[CustomContext, CustomState], ) -> Command: """Look up and update user info.""" - user_id = runtime.context.user_id + user_id = runtime.context.user_id name = "John Smith" if user_id == "user_123" else "Unknown user" return Command(update={ # [!code highlight] "user_name": name, @@ -737,7 +737,7 @@ agent = create_agent( model="gpt-5-nano", tools=[update_user_info, greet], state_schema=CustomState, # [!code highlight] - context_schema=CustomContext, + context_schema=CustomContext, ) agent.invoke(