Skip to content

πŸ€– Explain with AI

Setting Up Smart Context Injection for Your Codebase

Overview

Smart Context Injection is what separates 4Geeks AI Studio from generic AI coding tools. Unlike standard tools that look at code snippets in isolation, the AI Factory maps your entire codebase and injects project-wide context into every request β€” ensuring the AI understands your specific architecture, naming conventions, and business logic.

In this tutorial, you’ll learn:

  • How Smart Context Injection works
  • How to prepare your codebase for optimal context mapping
  • How to configure context rules and conventions
  • How to verify context quality in generated code

How Smart Context Injection Works

Your Repository
    β”‚
    β–Ό
Codebase Mapping
    β”œβ”€β”€ File structure analysis
    β”œβ”€β”€ Dependency graph construction
    β”œβ”€β”€ Architecture pattern detection
    β”œβ”€β”€ Naming convention extraction
    └── Business logic identification
    β”‚
    β–Ό
Context Index Creation
    β”œβ”€β”€ Module relationships
    β”œβ”€β”€ API contracts
    β”œβ”€β”€ Data models
    β”œβ”€β”€ Configuration patterns
    └── Coding standards
    β”‚
    β–Ό
Context Injection (per task)
    β”œβ”€β”€ Relevant modules identified
    β”œβ”€β”€ Related code snippets included
    β”œβ”€β”€ Architecture constraints applied
    └── Naming conventions enforced
    β”‚
    β–Ό
AI Code Generation (with full context)

Step 1: Connect Your Repository

  1. Go to your AI Studio project settings
  2. Navigate to Repository Connection
  3. Connect your repository:
  4. GitHub: Authorize 4Geeks to access your repo
  5. GitLab: Provide your GitLab instance URL and token
  6. Bitbucket: Connect via OAuth or app password
  7. Select the branches to include (typically main + active feature branches)

Step 2: Configure Context Rules

Architecture Documentation

Provide or create an architecture document that describes:

# Project Architecture

## Tech Stack
- Frontend: React 18, TypeScript, Tailwind CSS
- Backend: Node.js, Express, PostgreSQL
- Infrastructure: AWS (ECS, RDS, S3)

## Directory Structure
/src
  /components    # Reusable UI components
  /pages         # Route-level components
  /services      # API clients and business logic
  /hooks         # Custom React hooks
  /utils         # Utility functions
  /types         # TypeScript type definitions

## Key Patterns
- Feature-based module organization
- Repository pattern for data access
- Service layer for business logic
- Custom hooks for shared state logic

Coding Standards

Define your coding conventions:

# Coding Standards

## Naming Conventions
- Components: PascalCase (UserCard.tsx)
- Functions: camelCase (getUserById)
- Constants: UPPER_SNAKE_CASE (MAX_RETRIES)
- Types/interfaces: PascalCase with I prefix (IUser)

## Code Style
- 2-space indentation
- Single quotes for strings
- Semicolons required
- Max line length: 100 characters

## Testing
- Unit tests: Jest, colocated with source files
- Integration tests: Separate /tests directory
- E2E tests: Playwright in /e2e directory

API Contracts

If you have OpenAPI/Swagger specs, GraphQL schemas, or protobuf definitions, provide them:

/api
  openapi.yaml      # REST API specification
  graphql/
    schema.graphql  # GraphQL schema

Step 3: Set Context Priority Levels

Not all code is equally important for context. Configure priority levels:

Priority What’s Included When Used
Critical Core architecture, shared utilities, type definitions Every task
High Related modules, API contracts, database schemas Most tasks
Medium Similar components, related services Feature-specific tasks
Low Historical code, deprecated modules Rarely included

Configuration

  1. Go to AI Factory Settings β†’ Context Configuration
  2. Set priority rules:
  3. Always include: Core types, shared utilities, config files
  4. Include when relevant: Related modules, API endpoints
  5. Exclude: node_modules, dist, build artifacts, test fixtures
  6. Define file patterns:
    Always include:
    - src/types/**/*.ts
    - src/utils/**/*.ts
    - src/config/**/*
    
    Include when relevant:
    - src/components/**/*.tsx
    - src/services/**/*.ts
    
    Exclude:
    - **/*.test.ts
    - **/*.spec.ts
    - node_modules/**
    - dist/**
    

Step 4: Verify Context Quality

After your Senior Architect completes the initial mapping:

Check the Context Report

  1. Go to AI Factory β†’ Context Report
  2. Review:
  3. Mapped files: Total files indexed for context
  4. Dependency graph: Visual representation of module relationships
  5. Architecture patterns: Detected patterns (MVC, Repository, etc.)
  6. Naming conventions: Extracted conventions
  7. Coverage: % of codebase successfully mapped

Test Context Injection

Submit a test task and verify:

  1. Architecture awareness: Does the generated code follow your patterns?
  2. Naming consistency: Are conventions applied correctly?
  3. Import accuracy: Are imports from the correct modules?
  4. Type usage: Are existing types reused instead of redefined?

Best Practices

Prepare Your Codebase

  • Clean up before connecting: Remove dead code, fix obvious issues
  • Document key decisions: Add ADRs (Architecture Decision Records)
  • Maintain type definitions: TypeScript types are crucial for context
  • Keep tests separate: Exclude test files from context to reduce noise

Optimize Context Size

  • Be selective: Only include what’s necessary for code generation
  • Use abstractions: High-level architecture docs are more valuable than every file
  • Update regularly: Re-run context mapping after major refactors
  • Monitor token usage: Larger context = more tokens = higher cost

Common Issues and Solutions

Issue Solution
AI generates code that doesn’t match patterns Add explicit pattern documentation to context
Incorrect imports Verify dependency graph accuracy
Redefined types instead of reused Ensure type files are in “Always include”
Too many tokens consumed Reduce context scope, exclude unnecessary files
Missing business logic context Add domain-specific documentation

What’s Next?

Need Help?


Still questions? Ask the community.