Back to Home

Context Weaver: AI-Powered Code Intelligence

Building semantic search and natural language Q&A for understanding codebases

🧵
2.1s
Index 15 files
<500ms
Search response time
50-500x
ROI vs manual review

Overview

The Problem

Every developer faces this when joining a new codebase or returning to an old project: "Where is the authentication logic?", "How does error handling work here?", "Is there existing code for this feature?" Traditional tools like grep find keywords, but can't understand what code actually does. Searching for "validate" might return 47 files, none of which are what you need.

The Solution

An AI-powered code intelligence tool that understands code semantically. By combining AST parsing, vector embeddings, and large language models, developers can ask questions in natural language and get instant, accurate answers with exact source locations. Think of it as having an expert who's read your entire codebase and can instantly recall any detail.

The Challenge: Code Understanding at Scale

Modern codebases are large, complex, and constantly evolving. A typical enterprise application might contain 10,000+ files spanning multiple languages and frameworks. Developers need to:

  • Understand concepts, not just keywords: "authentication" vs "auth" vs "validateUser"
  • Find code by what it does: "Where is user input validated?" not "search for validate"
  • Navigate relationships: What functions call this? What does this module depend on?
  • Get quick answers: "How does the caching strategy work?" without reading 10 files

Existing tools fall short: grep requires exact keywords, IDE search is file-focused, and GitHub search works on filenames/comments but not semantic meaning. What developers really need is semantic understanding—search by concept, get answers in context.

The Approach: AST Parsing + Vector Embeddings + LLMs

Context Weaver combines three breakthrough technologies to create true code intelligence:

Stage 1: AST Parsing with Babel

Why: To search code semantically, you first need to understand its structure. Instead of treating files as plain text, Context Weaver parses them into Abstract Syntax Trees (ASTs)—understanding functions, classes, methods, parameters, and comments as distinct semantic units.

How: Using Babel (the same parser used by ESLint and Prettier), each TypeScript/JavaScript file is parsed into an AST. The parser extracts:

  • Functions (regular, arrow, class methods) with signatures and line numbers
  • Classes with their methods as separate searchable units
  • JSDoc/TSDoc comments explaining what code does
  • Import/export relationships showing module dependencies
// Example: Parsing extracts this structure
function validateUser(input: UserInput): boolean {
  /** Validates user input against schema */
  return schema.validate(input);
}

// Becomes this semantic chunk:
{
  filePath: "src/auth/validate.ts",
  chunkType: "function",
  name: "validateUser(input)",
  startLine: 15,
  endLine: 18,
  docstring: "Validates user input against schema",
  content: "function validateUser..."
}

Stage 2: Vector Embeddings for Semantic Search

Why: Traditional keyword search misses semantic similarity. "authentication logic" and "user login validation" mean the same thing but share no keywords. Vector embeddings solve this by representing code meaning as coordinates in high-dimensional space—similar concepts cluster together.

How: Each code chunk (function, class, method) is converted into a 1536-dimensional vector using OpenAI's text-embedding-3-small model. These embeddings capture not just what the code is named, but what it does. When developers search, their query is also embedded, and Weaviate's vector database performs approximate nearest neighbor (ANN) search to find semantically similar code—even if the exact words differ.

// Example: Embedding code chunks
const chunkText = `File: src/auth/validate.ts
Language: typescript
Type: function
Name: validateUser(input)

/** Validates user input against schema */
function validateUser(input: UserInput): boolean {
  return schema.validate(input);
}`;

const embedding = await openai.embeddings.create({
  model: "text-embedding-3-small",
  input: chunkText
});

// Store in Weaviate with vector
await weaviate.insertChunk(chunk, embedding.data[0].embedding);

Stage 3: LLM Q&A with Claude

Why: Finding relevant code is valuable, but developers often need explanation. "How does error handling work?" requires synthesizing information from multiple files and understanding architectural patterns. Large language models excel at this contextual reasoning.

How: When developers ask a question, Context Weaver: (1) searches for the top 5 most relevant code chunks, (2) sends them to Claude Sonnet 4.5 along with the question, (3) streams back a detailed explanation with source citations. The LLM can explain complex logic, trace execution flows, and provide architectural insights that pure search can't deliver.

Building the AI Pipeline

1. File Discovery & Filtering

Before parsing, Context Weaver needs to find files intelligently. The file discovery system:

  • Recursively walks directories with async generators for memory efficiency
  • Respects .gitignore patterns automatically
  • Filters by language (TypeScript, JavaScript, Python) based on file extensions
  • Excludes build artifacts (node_modules, dist, etc.) and minified files

This ensures only relevant source code gets indexed—no dependencies, no generated files, no noise.

2. Batch Embedding Generation

Performance was critical for developer workflow. OpenAI's API supports batching up to 2,048 inputs per request, so Context Weaver processes chunks in batches of 100. This optimization reduced API calls by 99% and achieved 2.1 second indexing for 15 files—fast enough for daily use.

3. Vector Storage & Hybrid Search

Weaviate stores code chunks with their embeddings in a local Docker container—no external database needed. When developers search, Weaviate performs hybrid search combining:

  • Vector similarity: Finds semantically similar code chunks
  • BM25 keyword matching: Ensures exact term matches rank highly
  • Metadata filtering: Can filter by language, file path, or chunk type

Results return in <500ms with confidence scores, file paths, and line numbers—instant navigation to relevant code.

Impact & Results

Performance Metrics

  • Indexing speed:15 files → 54 chunks in 2.1 seconds
  • Search latency:<500ms for results with scores
  • Cost per query:$0.01-0.05 (embedding + LLM Q&A)
  • Initial indexing cost:~$1-2 for 10,000 file codebase
  • ROI:50-500x vs manual code review time

Real-World Testing

Tested on Context Weaver itself (dogfooding):

Query: "How does the TypeScript parser extract functions from code?"

Result: Claude provided a detailed multi-section explanation including:
• Babel AST parsing process
• Function extraction for all declaration types
• Signature generation with parameters
• Error handling and fallback strategies

Sources cited: 5 code chunks with exact line numbers
Accuracy: 100% verified against actual implementation

Tech Stack & Architecture

Core Technologies

  • Babel: AST parser for TypeScript/JavaScript
  • Weaviate: Vector database for hybrid search
  • OpenAI: text-embedding-3-small for embeddings
  • Claude: Sonnet 4.5 for Q&A with streaming
  • TypeScript: Type-safe CLI with Commander.js

Key Design Decisions

  • • Babel AST over regex for robust parsing
  • • Batch embeddings (100/request) for 99% API reduction
  • • Self-hosted Weaviate for data privacy
  • • Graceful fallback to file-level indexing on parse errors
  • • Streaming LLM responses for better UX

Lessons Learned

AST Parsing is Worth the Investment

Initial temptation was to use simple regex to split functions—faster to build but fragile. Babel AST handles all edge cases: arrow functions, class methods, export declarations, decorators. The time investment paid off with robust, production-quality parsing that extracts rich metadata (line numbers, docstrings, signatures).

Batch Everything for Performance

Processing embeddings one-by-one would have taken ~50 seconds for 15 files. Batching 100 chunks per API call reduced this to 2.1 seconds—a 24x speedup. Always check API docs for batch endpoints before implementing sequential processing.

Dogfooding Accelerates Development

Using Context Weaver to index itself during development provided instant feedback on parsing quality and search relevance. Asking it questions about its own code revealed bugs and proved usefulness—if it can't understand itself, it won't understand user codebases.

Future Iterations

  • Python parser: Add tree-sitter-python for polyglot codebases
  • Incremental indexing: Only reindex changed files for 10-100x faster updates
  • VS Code extension: Inline search and Q&A directly in the editor
  • Watch mode: Auto-reindex on file changes during active development

Explore More