Skip to Content

dev_patterns

Analyze coding patterns in a file against similar code in the codebase. Compares import style, error handling, type coverage, test coverage, and file size.

Not for finding code (use dev_search) or tracing calls (use dev_refs).

Usage

dev_patterns(filePath, format?, limit?)

Parameters

ParameterTypeDefaultDescription
filePathstringrequiredFile path to analyze (e.g., src/auth/middleware.ts)
format"compact" | "verbose" | "json""compact"Output format
limitnumber10Maximum similar files to analyze

What It Does

dev_patterns performs comprehensive file analysis in two steps:

  1. Finds similar files — Uses semantic search to find code with similar structure/purpose
  2. Analyzes patterns — Compares error handling, type coverage, imports, testing, and file size

Examples

Basic Inspection

“Use dev_patterns with filePath ‘src/auth/middleware.ts‘“

## File Inspection: src/auth/middleware.ts ### Similar Files (5 analyzed) 1. `src/auth/session.ts` (78%) 2. `src/middleware/logger.ts` (72%) 3. `src/api/auth-handler.ts` (68%) ### Pattern Analysis **Import Style:** Your file uses `esm`, matching 100% of similar files. **Error Handling:** Your file uses `throw`, but 60% of similar files use `result`. **Type Coverage:** Your file has `full` type coverage, matching 80% of similar files. **Testing:** No test file found. 40% of similar files have tests. **Size:** 234 lines (smaller than average of 312 lines)

Verbose Output

“Use dev_patterns with filePath ‘packages/core/src/indexer/index.ts’, format ‘verbose’”

Returns detailed pattern distribution and statistics for each pattern category.

JSON Output (Token-Efficient)

“Use dev_patterns with filePath ‘src/auth/middleware.ts’, format ‘json’”

Returns structured JSON — compact and machine-parseable for agent workflows:

{ "fileSize": { "yourFile": 234, "average": 312, "deviation": "smaller" }, "importStyle": { "yourFile": "esm", "common": "esm", "percentage": 100 }, "errorHandling": { "yourFile": "throw", "common": "result", "percentage": 60 }, "typeAnnotations": { "yourFile": "full", "common": "full", "percentage": 80 }, "testing": { "yourFile": false, "percentage": 40 } }

Pattern Categories

PatternWhat It Checks
Import Styleesm, cjs, mixed, or unknown
Error Handlingthrow, result, error-return, or unknown
Type Coveragefull, partial, minimal, or none (TypeScript only)
TestingWhether a co-located test file exists
File SizeLine count vs. similar files

Workflow

Typical usage pattern:

  1. Search for concepts: dev_search { query: "authentication middleware" }
  2. Inspect what you found: dev_patterns { filePath: "src/auth/middleware.ts" }
  3. Analyze dependencies: dev_refs { name: "authMiddleware" }

Performance

Pattern analysis reads from the Antfly index when available (~100ms). Falls back to ts-morph scanning when not (~1-3s).

Migration from dev_explore

If you previously used dev_explore:

  • dev_explore { action: "similar", query: "file.ts" }dev_patterns { filePath: "file.ts" }
  • dev_explore { action: "validate", query: "file.ts" }dev_patterns { filePath: "file.ts" } (now does both!)
  • dev_explore { action: "pattern" } → Use dev_search instead
  • dev_explore { action: "relationships" } → Use dev_refs instead
Last updated on