Skip to main content
AG-Kit’s memory system is designed to seamlessly integrate with AI agents, providing automatic context management, persistent knowledge storage, and intelligent memory operations. This guide covers integration patterns, framework-specific implementations, and best practices.

Integration Overview

Memory-Agent Workflow

Core Integration Patterns

1. Automatic Memory Management
  • Agent automatically stores conversation events
  • Context retrieval happens transparently
  • Token limits managed automatically
2. Dual-Layer Memory
  • Short-term: Recent conversation context
  • Long-term: Persistent user knowledge and preferences
3. Intelligent Context Engineering
  • Automatic summarization for long conversations
  • Token-aware context trimming
  • Conversation branching for experimentation

Framework Integration

from langgraph.graph import StateGraph
from agkit.storage.langgraph import TDAICheckpointSaver

# Create TDAI checkpoint saver
checkpoint_saver = TDAICheckpointSaver(
    api_key=os.getenv('TDAI_API_KEY'),
    base_url=os.getenv('TDAI_BASE_URL'),
    checkpoint_type='checkpoints',
    checkpoint_writes_type='checkpoint_writes'
)

# Create graph with checkpoint saver
graph = StateGraph({
    'messages': []
})
graph.add_node('agent', agent_node)
graph.add_edge('__start__', 'agent')

compiled_graph = graph.compile(
    checkpointer=checkpoint_saver
)

# Run with thread persistence
result = await compiled_graph.ainvoke(
    {'messages': [{'role': 'user', 'content': 'Hello'}]},
    config={'configurable': {'thread_id': 'user-123'}}
)

Configuration & Best Practices

Memory Configuration

// Production configuration
const memory = MySQLMemory.create({
  host: process.env.DB_HOST,
  database: process.env.DB_NAME,
  sessionId: userId,

  // Context engineering
  maxTokens: 8000,
  enableContextManagement: true
});

const agent = new Agent({
  name: 'production-assistant',
  model: provider,
  memory: memory,

  // Memory-aware configuration
  maxContextTokens: 8000,
  memoryRetrievalLimit: 10
});

Troubleshooting

Common Integration Issues

Memory Not Persisting
# ❌ Wrong: Memory instance not shared
agent1 = Agent(memory=InMemoryMemory())
agent2 = Agent(memory=InMemoryMemory())

# ✅ Correct: Shared memory instance
shared_memory = InMemoryMemory()
agent1 = Agent(memory=shared_memory)
agent2 = Agent(memory=shared_memory)
Session Isolation Issues
# ❌ Wrong: Same session for different users
agent = Agent(memory=memory, session_id='default')

# ✅ Correct: Unique session per user
agent = Agent(
    memory=memory,
    session_id=f'user-{user_id}'
)

Next Steps