-
Notifications
You must be signed in to change notification settings - Fork 59
Code Examples
geoffrey fernald edited this page Jan 28, 2026
·
1 revision
Get real code snippets from your codebase that demonstrate how patterns are implemented. Essential for AI agents to generate code that matches your conventions.
Instead of generic examples, drift_code_examples returns actual code from YOUR codebase showing how patterns are used in practice.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CODE EXAMPLES β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Pattern: "api-rest-controller" β
β β
β Example 1: src/controllers/users.ts:12-45 β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β @Controller('/api/users') β β
β β export class UsersController { β β
β β @Get('/:id') β β
β β async getUser(@Param('id') id: string) { β β
β β const user = await this.userService.findById(id); β β
β β return { data: user, meta: { timestamp: Date.now() } }; β β
β β } β β
β β } β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Example 2: src/controllers/orders.ts:8-38 β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β @Controller('/api/orders') β β
β β export class OrdersController { β β
β β // Similar pattern... β β
β β } β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
drift_code_examples({
categories: ["api", "auth"], // Filter by categories
pattern: "api-rest-controller", // Specific pattern ID
maxExamples: 3, // Examples per pattern
contextLines: 10 // Lines of context
})Response:
{
"summary": "api, auth: 6 code examples from 2 patterns.",
"data": {
"examples": [
{
"patternId": "api-rest-controller",
"patternName": "REST Controller Pattern",
"category": "api",
"file": "src/controllers/users.ts",
"line": 12,
"code": "> 12 | @Controller('/api/users')\n 13 | export class UsersController {\n 14 | @Get('/:id')\n 15 | async getUser(@Param('id') id: string) {\n 16 | const user = await this.userService.findById(id);\n 17 | return { data: user, meta: { timestamp: Date.now() } };\n 18 | }\n 19 | }",
"explanation": "REST controller pattern for handling HTTP requests"
},
{
"patternId": "auth-middleware-usage",
"patternName": "Auth Middleware Pattern",
"category": "auth",
"file": "src/middleware/auth.ts",
"line": 5,
"code": "> 5 | export const requireAuth = async (req, res, next) => {\n 6 | // Auth middleware implementation...\n 7 | }",
"explanation": "Authentication middleware pattern"
}
],
"patternsFound": 2,
"examplesReturned": 6
},
"hints": {
"nextActions": [
"Use these examples as templates for new code",
"Use drift_pattern_get for more details on specific patterns"
],
"relatedTools": ["drift_pattern_get", "drift_patterns_list"]
}
}drift export --format ai-context --categories api,auth --snippetsdrift where api-rest-controller --verbose// AI agent workflow
const context = await drift_context({
intent: "add_feature",
focus: "user authentication"
});
// Get real examples
const examples = await drift_code_examples({
categories: ["auth"],
maxExamples: 3
});
// Now generate code following the examples// New developer wants to understand API patterns
drift_code_examples({
categories: ["api"],
maxExamples: 5,
contextLines: 15
})// Reviewer wants to show correct pattern
drift_code_examples({
pattern: "error-handling-try-catch",
maxExamples: 2
})drift_code_examples({
pattern: "api-rest-controller",
maxExamples: 1,
contextLines: 5
}){
"summary": "1 code examples from 1 patterns.",
"data": {
"examples": [{
"patternId": "api-rest-controller",
"patternName": "REST Controller Pattern",
"category": "api",
"file": "src/controllers/users.ts",
"line": 12,
"code": "> 12 | @Controller('/api/users')..."
}],
"patternsFound": 1,
"examplesReturned": 1
}
}drift_code_examples({
categories: ["api"],
maxExamples: 3,
contextLines: 15
})Returns more context lines around each pattern match with explanations.
The drift_context tool can guide you to relevant examples:
drift_context({
intent: "add_feature",
focus: "REST endpoint"
})Then use drift_code_examples for detailed snippets:
// After getting pattern IDs from drift_context
drift_code_examples({
pattern: "api-rest-controller",
maxExamples: 3
})// This gives you pattern IDs
const context = await drift_context({ intent: "add_feature", focus: "auth" });
// Then get detailed examples
const examples = await drift_code_examples({
pattern: context.relevantPatterns[0].id
});// Don't request all categories
drift_code_examples({ categories: ["api"] }) // β
Good
// Avoid
drift_code_examples({}) // β Too broad, wastes tokens// Usually 2-3 examples are enough
drift_code_examples({
pattern: "error-handling",
maxExamples: 3 // β
Sufficient
})// More context for complex patterns
drift_code_examples({
pattern: "transaction-patterns",
contextLines: 20 // Need more context for transactions
})
// Less context for simple patterns
drift_code_examples({
pattern: "log-levels",
contextLines: 5 // Simple pattern, less context needed
})- Similar Code Search β Find semantically similar code
- Pattern Categories β All 15 categories
- MCP Tools Reference β All 50 tools
- Cortex V2 Overview
- Memory Setup Wizard
- Memory CLI
- Universal Memory Types
- Learning System
- Token Efficiency
- Causal Graphs
- Code Generation
- Predictive Retrieval
- Architecture
- Call Graph Analysis
- Impact Analysis
- Security Analysis
- Data Boundaries
- Test Topology
- Coupling Analysis
- Error Handling Analysis
- Wrappers Detection
- Environment Variables
- Constants Analysis
- Styling DNA
- Constraints
- Contracts
- Decision Mining
- Speculative Execution
- Watch Mode
- Trends Analysis
- Projects Management
- Package Context
- Monorepo Support
- Reports & Export
- Dashboard
- 10 Languages
- 21 Frameworks
- 16 ORMs
- 400+ Detectors
- 50+ MCP Tools
- 60+ CLI Commands
- 23 Memory Types