Skip to content

Conversation

sdip0971
Copy link
Contributor

Description

This PR adds optional TTL (Time-To-Live) support to the MongoDB-backed checkpoint saver in the @langchain/langgraph-checkpoint package.


What's New

✅ Added enableTTL param in constructor:

export type MongoDBSaverParams = {
  client: MongoClient;
  dbName?: string;
  checkpointCollectionName?: string;
  checkpointWritesCollectionName?: string;

  // ✅ Change 1: Optional TTL toggle
  enableTTL?: boolean;
};

✅ Stored enableTTL in class and defaulted it:

this.enableTTL = enableTTL ?? false;

✅ Conditionally included TTL timestamp during checkpoint put:

const doc = {
  parent_checkpoint_id: config.configurable?.checkpoint_id,
  type: checkpointType,
  checkpoint: serializedCheckpoint,
  metadata: serializedMetadata,

  // ✅ Change 2: TTL timestamp only if enabled
  ...(this.enableTTL ? { _createdAtForTTL: new Date() } : {}),
};

Why This Matters

In long-running or high-frequency LangGraph applications, stale checkpoints can accumulate and consume storage.
This update allows users to opt-in to automatic cleanup by using MongoDB’s native TTL index on the _createdAtForTTL field.


Example TTL Index Creation (Manual Step)

// 🔁 This will auto-delete checkpoints after 1 hour
db.checkpoints.createIndex(
  { _createdAtForTTL: 1 },
  { expireAfterSeconds: 3600 }
);

Non-Breaking

  • TTL is fully opt-in
  • Existing functionality is untouched if enableTTL is not set
  • Zero breaking changes for existing users or systems

Final Notes

Massive thanks to the LangGraph team for making such a cool platform 🙌
If you'd like to credit or tag me for the contribution, here’s my handle:

GitHub: @sdip0971

Copy link

changeset-bot bot commented Jul 15, 2025

⚠️ No Changeset found

Latest commit: 76daaf7

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@b-steel
Copy link

b-steel commented Jul 15, 2025

@sdip0971 Was just looking for this, thanks for beating me to the punch on it. Would it not be a good idea to include the checkpoint-writes collection in this as well so that a TTL index can be created there too?

Another note looking at your code, it seems like this includes your other PR, just FYI

@yishaistern
Copy link

@sdip0971 thanks for taking this 👍 , if it is ok to addd to what @b-steel wrote above , the DB for the checkpoint-writes is with a lot as docs as well

@dqbd dqbd force-pushed the feat/mongodb-ttl-support branch from 8d6cce9 to 591f098 Compare September 2, 2025 12:45
@dqbd dqbd changed the title Feat/mongodb ttl support feat(mongo): add TTL support via MongoDB index Sep 2, 2025
@dqbd dqbd changed the title feat(mongo): add TTL support via MongoDB index feat(checkpoint-mongo): add TTL support via MongoDB index Sep 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants