Master the art of building, optimizing, and managing context for AI agents. Learn how to provide the right information at the right time.
Context is the complete information environment that AI agents need to understand your project and generate relevant, accurate code. It includes your codebase, documentation, dependencies, history, and runtime information.
Build rich context that gives AI agents complete understanding of your project:
// Building Comprehensive Context
import { ContextManager } from '@mcpcodex/sdk';
const context = new ContextManager({
projectId: 'my-project',
maxTokens: 128000, // Claude 3 context window
compressionLevel: 'intelligent' // automatic, aggressive, or none
});
// Add project files
await context.addFiles({
patterns: [
'src/**/*.{ts,tsx}',
'package.json',
'tsconfig.json',
'README.md'
],
exclude: [
'node_modules',
'dist',
'**/*.test.ts',
'**/*.spec.ts'
],
prioritize: [
'src/index.ts', // Entry points first
'src/api/**/*', // Critical paths
'src/types/**/*' // Type definitions
]
});
// Add git history for context
await context.addGitHistory({
branches: ['main', 'develop'],
commits: 50, // Last 50 commits
includeMessages: true,
includeDiffs: false // Too verbose for context
});
// Add external documentation
await context.addDocumentation({
sources: [
{ type: 'url', path: 'https://docs.react.dev' },
{ type: 'file', path: './docs/architecture.md' },
{ type: 'api', path: 'https://api.example.com/swagger.json' }
]
});
// Add runtime information
await context.addRuntimeInfo({
environment: process.env.NODE_ENV,
dependencies: require('./package.json').dependencies,
configuration: require('./config'),
metrics: {
performance: await getPerformanceMetrics(),
errors: await getRecentErrors(),
usage: await getUsageStats()
}
});
// Build optimized context
const optimizedContext = await context.build({
strategy: 'relevance-ranked',
maxSize: 100000, // tokens
preserveStructure: true
});
Efficiently manage limited context windows to maximize AI performance:
// Context Window Management
class ContextWindowManager {
constructor(config) {
this.maxTokens = config.maxTokens || 128000;
this.reservedTokens = config.reservedTokens || 4000; // For response
this.compressionRatio = config.compressionRatio || 0.75;
this.chunks = [];
}
// Calculate available context space
getAvailableSpace() {
const usedTokens = this.chunks.reduce((sum, chunk) =>
sum + chunk.tokenCount, 0
);
return this.maxTokens - this.reservedTokens - usedTokens;
}
// Add content with priority
addContent(content, priority = 5) {
const tokens = this.tokenize(content);
const chunk = {
content,
tokens,
tokenCount: tokens.length,
priority,
timestamp: Date.now()
};
// Check if it fits
if (this.getAvailableSpace() >= chunk.tokenCount) {
this.chunks.push(chunk);
this.chunks.sort((a, b) => b.priority - a.priority);
return true;
}
// Try to make room by removing low-priority chunks
return this.makeRoom(chunk);
}
// Intelligent compression
compress(content) {
// Remove comments
content = content.replace(/\/\/.*$/gm, '');
content = content.replace(/\/\*[\s\S]*?\*\//g, '');
// Minify whitespace
content = content.replace(/\s+/g, ' ');
// Remove non-essential code
content = this.removeNonEssential(content);
return content;
}
// Sliding window for streaming
createSlidingWindow(streamSize = 10000) {
return {
window: [],
maxSize: streamSize,
slide(newContent) {
const tokens = this.tokenize(newContent);
this.window.push(...tokens);
// Keep only recent context
if (this.window.length > this.maxSize) {
this.window = this.window.slice(-this.maxSize);
}
return this.window;
},
getContext() {
return this.window.join('');
}
};
}
// Smart truncation
truncate(content, maxTokens) {
const sections = this.identifySections(content);
const prioritized = this.prioritizeSections(sections);
let result = '';
let tokenCount = 0;
for (const section of prioritized) {
const sectionTokens = this.tokenize(section.content).length;
if (tokenCount + sectionTokens <= maxTokens) {
result += section.content;
tokenCount += sectionTokens;
} else {
// Partial inclusion with ellipsis
const remaining = maxTokens - tokenCount;
const partial = this.tokenize(section.content)
.slice(0, remaining)
.join('');
result += partial + '\n...truncated...\n';
break;
}
}
return result;
}
}
Use semantic understanding to provide more intelligent context:
// Semantic Context Enhancement
class SemanticContextBuilder {
constructor(embeddings) {
this.embeddings = embeddings;
this.semanticIndex = new Map();
}
// Build semantic understanding
async buildSemanticContext(codebase) {
const context = {
entities: [],
relationships: [],
patterns: [],
dependencies: []
};
// Extract entities (classes, functions, variables)
for (const file of codebase.files) {
const ast = await this.parseAST(file);
// Extract semantic entities
const entities = this.extractEntities(ast);
for (const entity of entities) {
const embedding = await this.embeddings.encode(entity.code);
context.entities.push({
...entity,
embedding,
file: file.path,
importance: this.calculateImportance(entity)
});
}
// Extract relationships
const relationships = this.extractRelationships(ast);
context.relationships.push(...relationships);
}
// Identify patterns
context.patterns = await this.identifyPatterns(context.entities);
// Build dependency graph
context.dependencies = this.buildDependencyGraph(
context.entities,
context.relationships
);
return context;
}
// Find relevant context based on query
async findRelevantContext(query, semanticContext, topK = 10) {
const queryEmbedding = await this.embeddings.encode(query);
// Calculate similarity scores
const scores = semanticContext.entities.map(entity => ({
entity,
similarity: this.cosineSimilarity(queryEmbedding, entity.embedding),
connections: this.getConnections(entity, semanticContext.relationships)
}));
// Sort by relevance
scores.sort((a, b) => b.similarity - a.similarity);
// Get top K with their dependencies
const relevant = scores.slice(0, topK);
const expanded = this.expandWithDependencies(
relevant,
semanticContext.dependencies
);
return expanded;
}
// Pattern recognition
async identifyPatterns(entities) {
const patterns = {
architectural: [],
behavioral: [],
structural: []
};
// Identify architectural patterns
patterns.architectural = this.findArchitecturalPatterns(entities);
// Identify behavioral patterns
patterns.behavioral = this.findBehavioralPatterns(entities);
// Identify structural patterns
patterns.structural = this.findStructuralPatterns(entities);
return patterns;
}
// Calculate cosine similarity
cosineSimilarity(vec1, vec2) {
const dotProduct = vec1.reduce((sum, val, i) => sum + val * vec2[i], 0);
const mag1 = Math.sqrt(vec1.reduce((sum, val) => sum + val * val, 0));
const mag2 = Math.sqrt(vec2.reduce((sum, val) => sum + val * val, 0));
return dotProduct / (mag1 * mag2);
}
}
Identify and index all code entities for semantic search and retrieval.
Build dependency graphs and understand code relationships.
Identify architectural and design patterns in your codebase.
Adapt context in real-time based on agent needs and project changes:
// Dynamic Context Adaptation
class DynamicContextAdapter {
constructor() {
this.contextHistory = [];
this.feedbackLoop = new FeedbackLoop();
this.contextOptimizer = new ContextOptimizer();
}
// Adapt context based on agent feedback
async adaptContext(currentContext, agentFeedback) {
// Analyze what the agent is looking for
const needs = this.analyzeAgentNeeds(agentFeedback);
// Track context usage
this.trackContextUsage(currentContext, agentFeedback);
// Optimize based on patterns
const optimized = await this.contextOptimizer.optimize({
current: currentContext,
needs,
history: this.contextHistory,
feedback: this.feedbackLoop.getInsights()
});
return optimized;
}
// Real-time context updates
streamContextUpdates(projectWatcher) {
return new Observable(observer => {
// Watch for file changes
projectWatcher.on('change', async (file) => {
const update = await this.processFileChange(file);
observer.next({
type: 'file_change',
file: file.path,
update
});
});
// Watch for git changes
projectWatcher.on('commit', async (commit) => {
const update = await this.processCommit(commit);
observer.next({
type: 'commit',
commit: commit.hash,
update
});
});
// Watch for dependency updates
projectWatcher.on('dependency', async (dep) => {
const update = await this.processDependencyChange(dep);
observer.next({
type: 'dependency',
dependency: dep.name,
update
});
});
});
}
// Learn from context effectiveness
learnFromUsage(context, outcome) {
this.feedbackLoop.record({
context,
outcome,
timestamp: Date.now()
});
// Update optimization parameters
if (outcome.successful) {
this.contextOptimizer.reinforcePattern(context);
} else {
this.contextOptimizer.adjustStrategy(context, outcome.error);
}
}
// Predictive context loading
async predictiveLoad(task) {
const similar = this.findSimilarTasks(task);
const patterns = this.extractPatterns(similar);
// Preload likely needed context
const predicted = {
files: this.predictFiles(task, patterns),
documentation: this.predictDocs(task, patterns),
examples: this.predictExamples(task, patterns),
dependencies: this.predictDependencies(task, patterns)
};
return this.preloadContext(predicted);
}
}
Efficiently store and retrieve context for improved performance:
// Context Persistence and Caching
class ContextCache {
constructor(storage) {
this.storage = storage; // Redis, DynamoDB, etc.
this.memoryCache = new LRUCache({ max: 100 });
this.compressionEngine = new CompressionEngine();
}
// Save context snapshot
async saveSnapshot(projectId, context) {
const snapshot = {
id: generateSnapshotId(),
projectId,
timestamp: Date.now(),
context: await this.compressionEngine.compress(context),
metadata: {
files: context.files.length,
tokens: context.tokenCount,
hash: this.hashContext(context)
}
};
// Save to persistent storage
await this.storage.put(`context:${projectId}:${snapshot.id}`, snapshot);
// Update memory cache
this.memoryCache.set(projectId, snapshot);
return snapshot.id;
}
// Load context with delta updates
async loadWithDelta(projectId, baseSnapshotId) {
// Load base snapshot
const base = await this.storage.get(`context:${projectId}:${baseSnapshotId}`);
const context = await this.compressionEngine.decompress(base.context);
// Apply delta changes since snapshot
const deltas = await this.storage.query({
prefix: `delta:${projectId}`,
since: base.timestamp
});
for (const delta of deltas) {
context = this.applyDelta(context, delta);
}
return context;
}
// Incremental updates
async updateIncremental(projectId, changes) {
const delta = {
id: generateDeltaId(),
projectId,
timestamp: Date.now(),
changes
};
// Save delta
await this.storage.put(`delta:${projectId}:${delta.id}`, delta);
// Update memory cache if present
if (this.memoryCache.has(projectId)) {
const cached = this.memoryCache.get(projectId);
const updated = this.applyDelta(cached.context, delta);
this.memoryCache.set(projectId, { ...cached, context: updated });
}
return delta.id;
}
// Smart eviction policy
async evict() {
const stats = await this.getUsageStats();
// Identify candidates for eviction
const candidates = stats.filter(s =>
s.lastAccessed < Date.now() - 7 * 24 * 60 * 60 * 1000 && // 7 days old
s.accessCount < 5 // Rarely accessed
);
for (const candidate of candidates) {
await this.storage.delete(`context:${candidate.projectId}:*`);
this.memoryCache.delete(candidate.projectId);
}
}
}
Context management is crucial for AI performance. Get expert guidance on optimization.