Integrations
LangGraph Integration
Integrate Fold with LangGraph for automatic context optimization. Uses LangGraph's checkpointer interface for seamless integration with existing workflows.
Overview
Fold provides a FoldCheckpointer that implements LangGraph's BaseCheckpointSaver interface. This means you can add context optimization to any LangGraph workflow without changing your graph logic.
Automatic Optimization
Context is optimized at each checkpoint automatically. No changes to your nodes or edges.
Pluggable Storage
Use PostgreSQL, in-memory, or implement your own storage backend.
Time Travel
Full checkpoint history preserved. Replay from any point with optimized context.
Stats Per Thread
Track token savings per conversation thread with built-in analytics.
Installation
pnpm add @fold/sdkQuick Start
import { StateGraph } from "@langchain/langgraph"
import { FoldCheckpointer, MemoryStorage } from "@fold/sdk/langgraph"
// Create checkpointer with Fold optimization
const checkpointer = new FoldCheckpointer({
context: {
budget: 100_000,
model: "gpt-4o",
},
storage: new MemoryStorage(),
})
// Build your graph as normal
const graph = new StateGraph({ channels: { messages: [] } })
.addNode("agent", agentNode)
.addNode("tools", toolsNode)
.addConditionalEdges("agent", shouldContinue)
.addEdge("tools", "agent")
.compile({ checkpointer }) // <-- Use Fold checkpointer
// Run with context optimization
const result = await graph.invoke(
{ messages: [{ role: "user", content: "Hello!" }] },
{ configurable: { thread_id: "conversation-123" } }
)
// Check savings for this thread
const stats = checkpointer.getStats("conversation-123")
console.log(stats)
// { tokensSaved: 5000, savingsPercent: 45 }Storage Backends
In-Memory Storage
Best for development and testing. Data is lost when the process exits.
import { FoldCheckpointer, MemoryStorage } from "@fold/sdk/langgraph"
const checkpointer = new FoldCheckpointer({
context: { budget: 100_000 },
storage: new MemoryStorage(),
})PostgreSQL Storage
Production-ready storage using PostgreSQL. Works with Neon, Supabase, or any Postgres database.
import { FoldCheckpointer, PostgresStorage } from "@fold/sdk/langgraph"
import { drizzle } from "drizzle-orm/neon-http"
const db = drizzle(process.env.DATABASE_URL)
const checkpointer = new FoldCheckpointer({
context: { budget: 100_000 },
storage: new PostgresStorage(db),
})Custom Storage
Implement the CheckpointStorage interface for custom backends:
import type { CheckpointStorage } from "@fold/sdk/langgraph"
class RedisStorage implements CheckpointStorage {
async get(threadId: string, checkpointId?: string) {
// Return checkpoint data
}
async put(threadId: string, checkpoint: StoredCheckpoint) {
// Store and return checkpoint ID
}
async *list(threadId: string, limit?: number) {
// Yield checkpoint history
}
async delete(threadId: string) {
// Delete thread data
}
}LangChain Memory
For simpler LangChain chains (not LangGraph), use ContextManagedMemory:
import { ContextManagedMemory } from '@fold/sdk/langchain'
import { ConversationChain } from 'langchain/chains'
import { ChatOpenAI } from '@langchain/openai'
const memory = new ContextManagedMemory({
budget: 100_000,
model: 'gpt-4o',
})
const chain = new ConversationChain({
llm: new ChatOpenAI({ modelName: 'gpt-4o' }),
memory,
})
await chain.call({ input: 'Hello!' })
await chain.call({ input: 'What did I just say?' })
console.log(memory.getStats())
// { turnCount: 2, tokensSaved: 0, savingsPercent: 0 }
// After many turns, you'll see savings
await chain.call({ input: '... many more messages ...' })
console.log(memory.getStats())
// { turnCount: 50, tokensSaved: 45000, savingsPercent: 68 }API Reference
FoldCheckpointer
LangGraph checkpointer with automatic context optimization
Constructor Options
| Option | Type | Description |
|---|---|---|
| context.budget | number | Token budget for optimization |
| context.model | string | Model for tokenization |
| context.window | number | Turns to keep unmasked |
| storage | CheckpointStorage | Storage backend instance |
Methods
getStats(threadId)- Get optimization stats for a threadgetSession(threadId)- Get the raw ContextSession for a threaddeleteThread(threadId)- Delete a thread and its checkpoints
ContextManagedMemory
LangChain memory with context optimization
Constructor Options
| Option | Type | Description |
|---|---|---|
| budget | number | Token budget |
| model | string | Model for tokenization |
| returnMessages | boolean | Return as message objects (default: true) |
Methods
getStats()- Get optimization statisticsclear()- Reset memory and session
Best Practices
Use consistent thread IDs
Use meaningful thread IDs (user ID, session ID, conversation ID) to track context across invocations.
Set budget below context limit
If your model has a 128K context window, set budget to ~100K to leave room for the response.
Monitor stats in production
Call
getStats() and log the savings to track cost reduction over time.