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¶
- Go to your AI Studio project settings
- Navigate to Repository Connection
- Connect your repository:
- GitHub: Authorize 4Geeks to access your repo
- GitLab: Provide your GitLab instance URL and token
- Bitbucket: Connect via OAuth or app password
- 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:
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¶
- Go to AI Factory Settings β Context Configuration
- Set priority rules:
- Always include: Core types, shared utilities, config files
- Include when relevant: Related modules, API endpoints
- Exclude: node_modules, dist, build artifacts, test fixtures
- Define file patterns:
Step 4: Verify Context Quality¶
After your Senior Architect completes the initial mapping:
Check the Context Report¶
- Go to AI Factory β Context Report
- Review:
- Mapped files: Total files indexed for context
- Dependency graph: Visual representation of module relationships
- Architecture patterns: Detected patterns (MVC, Repository, etc.)
- Naming conventions: Extracted conventions
- Coverage: % of codebase successfully mapped
Test Context Injection¶
Submit a test task and verify:
- Architecture awareness: Does the generated code follow your patterns?
- Naming consistency: Are conventions applied correctly?
- Import accuracy: Are imports from the correct modules?
- 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?¶
- Learn about Automated QA & Security Guardrails
- Explore Monitoring Token Usage
- Read about Understanding the AI Factory
Need Help?¶
- Documentation: docs.4geeks.io
- Community: community.4geeks.io
- Support: Available through the console dashboard
Still questions? Ask the community.