Integrations
Coding Agents
Learn how to use Fold with AI coding assistants like Claude Code, Cursor, and your own custom coding agents.
Understanding the Integration
Claude Code and Cursor are closed products that manage their own context windows. You cannot directly integrate Fold into these tools. However, Fold is designed for developers building their own coding agents or extending these tools via MCP.
When to Use Fold
Use Fold When...
- Building your own coding agent
- Creating an MCP server for Claude Code/Cursor
- Using Claude or OpenAI APIs directly
- Building VS Code extensions with AI features
- Creating CLI tools that interact with LLMs
Cannot Use Fold With...
- Claude Code CLI (closed product)
- Cursor IDE native chat (closed product)
- GitHub Copilot (closed product)
- Any tool where you don't control the API calls
Building Your Own Coding Agent
If you're building a coding agent similar to Claude Code or Cursor's agent mode, here's how to integrate Fold:
import Anthropic from '@anthropic-ai/sdk'
import { fold } from '@fold/sdk'
const anthropic = new Anthropic()
const ctx = fold("coding") // 100K budget, 15 turn window
// Define your coding tools
const codingTools = [
{
name: "read_file",
description: "Read a file from the filesystem",
input_schema: {
type: "object",
properties: {
path: { type: "string", description: "File path to read" }
},
required: ["path"]
}
},
{
name: "write_file",
description: "Write content to a file",
input_schema: {
type: "object",
properties: {
path: { type: "string", description: "File path to write" },
content: { type: "string", description: "Content to write" }
},
required: ["path", "content"]
}
},
{
name: "run_command",
description: "Run a shell command",
input_schema: {
type: "object",
properties: {
command: { type: "string", description: "Command to run" }
},
required: ["command"]
}
}
]
ctx.system(`You are a coding assistant with access to the filesystem.
You can read files, write files, and run shell commands.
Always explain your reasoning before taking action.`)
// Agent loop
while (true) {
const response = await anthropic.messages.create({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 4096,
messages: ctx.messages(), // Optimized context!
tools: codingTools,
})
// Process the response
for (const block of response.content) {
if (block.type === 'text') {
ctx.think(block.text)
console.log("Agent:", block.text)
}
if (block.type === 'tool_use') {
ctx.act(block.input, block.name)
console.log(`Using tool: ${block.name}`)
// Execute the tool
const result = await executeTool(block.name, block.input)
ctx.observe(result, block.name)
}
}
// Stop on loops, failures, or goal completion
if (ctx.stop()) {
console.log("Stopping:", ctx.reason())
break
}
// Check if the agent is done (no tool calls)
if (!response.content.some(b => b.type === 'tool_use')) {
break
}
}
console.log(ctx.saved())
// { tokens: 45000, percent: 68, cost: 0.45 }
async function executeTool(name: string, input: any): Promise<string> {
switch (name) {
case 'read_file':
return await fs.readFile(input.path, 'utf-8')
case 'write_file':
await fs.writeFile(input.path, input.content)
return `Successfully wrote to ${input.path}`
case 'run_command':
const { stdout, stderr } = await exec(input.command)
return stdout || stderr || 'Command completed'
default:
return 'Unknown tool'
}
}Using OpenAI GPT-4
The same pattern works with OpenAI's function calling:
import OpenAI from 'openai'
import { fold } from '@fold/sdk'
const openai = new OpenAI()
const ctx = fold("coding")
ctx.system("You are a coding assistant...")
while (true) {
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: ctx.messages(),
tools: openaiTools,
})
const message = response.choices[0].message
if (message.tool_calls?.length) {
for (const call of message.tool_calls) {
ctx.act(JSON.parse(call.function.arguments), call.function.name)
const result = await executeTool(call)
ctx.observe(result, call.function.name)
}
} else {
ctx.think(message.content)
}
if (ctx.stop()) break
}
console.log(ctx.saved())
// { tokens: 45000, percent: 68, cost: 0.45 }MCP Server Integration
Both Claude Code and Cursor support the Model Context Protocol (MCP). You can build an MCP server that uses Fold internally to manage persistent, optimized context across sessions:
import { Server } from "@modelcontextprotocol/sdk/server/index.js"
import { fold, restore } from '@fold/sdk'
// Store sessions by ID
const sessions = new Map<string, ReturnType<typeof fold>>()
const server = new Server({
name: "fold-context",
version: "1.0.0",
}, {
capabilities: {
tools: {},
},
})
// Tool to add context to a session
server.setRequestHandler("tools/call", async (request) => {
const { name, arguments: args } = request.params
if (name === "context_add") {
const { sessionId, content, type } = args
// Get or create session
let ctx = sessions.get(sessionId)
if (!ctx) {
ctx = fold("long-running")
sessions.set(sessionId, ctx)
}
// Add content based on type
switch (type) {
case 'thought':
ctx.think(content)
break
case 'action':
ctx.act(content, args.tool)
break
case 'observation':
ctx.observe(content, args.source)
break
}
return {
content: [{
type: "text",
text: JSON.stringify({
success: true,
saved: ctx.saved(),
turnCount: ctx.stats().turnCount
})
}]
}
}
if (name === "context_get") {
const { sessionId } = args
const ctx = sessions.get(sessionId)
if (!ctx) {
return {
content: [{
type: "text",
text: JSON.stringify({ error: "Session not found" })
}]
}
}
return {
content: [{
type: "text",
text: JSON.stringify({
messages: ctx.messages(),
stats: ctx.stats(),
saved: ctx.saved()
})
}]
}
}
if (name === "context_save") {
const { sessionId } = args
const ctx = sessions.get(sessionId)
if (!ctx) {
return {
content: [{
type: "text",
text: JSON.stringify({ error: "Session not found" })
}]
}
}
// Export for persistence
const data = ctx.save()
return {
content: [{
type: "text",
text: JSON.stringify(data)
}]
}
}
if (name === "context_restore") {
const { sessionId, data } = args
const ctx = restore(JSON.parse(data))
sessions.set(sessionId, ctx)
return {
content: [{
type: "text",
text: JSON.stringify({ success: true })
}]
}
}
})
// List available tools
server.setRequestHandler("tools/list", async () => {
return {
tools: [
{
name: "context_add",
description: "Add content to a Fold context session",
inputSchema: {
type: "object",
properties: {
sessionId: { type: "string" },
content: { type: "string" },
type: {
type: "string",
enum: ["thought", "action", "observation"]
},
tool: { type: "string" },
source: { type: "string" }
},
required: ["sessionId", "content", "type"]
}
},
{
name: "context_get",
description: "Get optimized messages from a session",
inputSchema: {
type: "object",
properties: {
sessionId: { type: "string" }
},
required: ["sessionId"]
}
},
{
name: "context_save",
description: "Export a session for persistence",
inputSchema: {
type: "object",
properties: {
sessionId: { type: "string" }
},
required: ["sessionId"]
}
},
{
name: "context_restore",
description: "Restore a session from saved data",
inputSchema: {
type: "object",
properties: {
sessionId: { type: "string" },
data: { type: "string" }
},
required: ["sessionId", "data"]
}
}
]
}
})
server.connect(transport)VS Code Extension
If you're building a VS Code extension with AI features, you can use Fold to manage context across interactions:
import * as vscode from 'vscode'
import { fold } from '@fold/sdk'
import OpenAI from 'openai'
// Create a session per workspace
const workspaceSessions = new Map<string, ReturnType<typeof fold>>()
export function activate(context: vscode.ExtensionContext) {
const openai = new OpenAI()
// Command: Ask AI about code
const askAI = vscode.commands.registerCommand('myext.askAI', async () => {
const workspaceId = vscode.workspace.workspaceFolders?.[0]?.uri.toString() ?? 'default'
// Get or create session for this workspace
let ctx = workspaceSessions.get(workspaceId)
if (!ctx) {
ctx = fold("coding")
ctx.system(`You are a coding assistant for VS Code.
You help with code explanation, debugging, and refactoring.`)
workspaceSessions.set(workspaceId, ctx)
}
// Get user input
const question = await vscode.window.showInputBox({
prompt: 'Ask the AI about your code'
})
if (!question) return
// Get current file context
const editor = vscode.window.activeTextEditor
if (editor) {
const selection = editor.document.getText(editor.selection)
if (selection) {
ctx.observe(`Selected code:
${selection}`, 'editor')
}
}
ctx.observe(question, 'user')
// Call the API with optimized context
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: ctx.messages(),
})
const answer = response.choices[0].message.content
ctx.think(answer)
// Show the response
vscode.window.showInformationMessage(answer ?? 'No response')
// Log savings
const saved = ctx.saved()
console.log(`Fold saved ${saved.percent.toFixed(0)}% tokens`)
})
context.subscriptions.push(askAI)
}Best Practices
Use the "coding" preset
The
fold("coding") preset is optimized for coding tasks with a 100K budget and 15-turn window, balancing context retention with cost efficiency.Check for stop signals
Always check
ctx.stop() in your agent loop. Fold detects repeated failures, loops, and goal completion to prevent wasted tokens.Persist sessions across restarts
Use
ctx.save() and restore(data) to maintain context across process restarts, especially for long-running agents.Categorize your content
Use
think(), act(), and observe() to help Fold understand which content can be aggressively compressed (observations) vs. preserved (decisions, errors).