Context Engineering
Overview
"Context is the single biggest lever for agent output quality." Agents don't read your mind — they read your context. This skill teaches you to engineer that context so agents produce code that matches your project's conventions, architecture, and constraints.
When to Use
- Setting up a new Android project for AI-assisted development
- Agent output consistently diverges from project conventions
- Agent invents APIs or patterns that don't exist in your codebase
- After adding new libraries, modules, or architectural patterns
- When onboarding a new team member who uses AI tools
Skip when: Agent output is already consistent with project conventions.
Five-Level Context Hierarchy
Load context in this priority order:
| Level |
Source |
What It Provides |
Persistence |
| 1 |
Rules files (CLAUDE.md, .cursorrules) |
Tech stack, commands, conventions, boundaries |
Permanent |
| 2 |
Specs & architecture docs (SPEC.md, ADRs) |
Design decisions, constraints, rationale |
Per-project |
| 3 |
Source code (read specific files) |
Current implementation, patterns in use |
Real-time |
| 4 |
Error output & test results |
What's broken, what's expected |
Per-session |
| 5 |
Conversation history |
Current task context |
Ephemeral |
Optimal range: ~2,000 lines of focused context per task. More dilutes attention; less causes invention.
Core Process
Step 1: Write Rules Files
- Create a
CLAUDE.md (or equivalent) in your project root:
# Project Rules
## Tech Stack
- Language: Kotlin 2.0+
- UI: Jetpack Compose with Material 3
- Architecture: MVVM with Clean Architecture layers
- DI: Hilt
- Async: Coroutines + Flow
- Database: Room
- Network: Retrofit + OkHttp + Kotlin Serialization
- Image loading: Coil
- Navigation: Navigation Compose
- Testing: JUnit5 + MockK + Compose Test Rules + Espresso
## Commands
- Build: `./gradlew assembleDebug`
- Test (unit): `./gradlew test`
- Test (instrumented): `./gradlew connectedAndroidTest`
- Lint: `./gradlew lint`
- Format: `./gradlew spotlessApply`
- Check: `./gradlew detekt`
## Module Structure
- `:app` — application module (MainActivity, navigation, DI setup)
- `:feature:*` — feature modules (screens, ViewModels)
- `:core:data` — repositories, data sources, API services
- `:core:domain` — use cases, domain models
- `:core:ui` — shared Compose components, theme
- `:core:common` — utilities, extensions
## Conventions
- ViewModels expose `StateFlow<UiState>`, never `LiveData`
- UI state is a single sealed interface per screen
- Repository functions are `suspend` or return `Flow`
- Use `@Inject constructor` for Hilt, not field injection
- Composables: stateless with state hoisting
- Tests follow Arrange-Act-Assert pattern
- Naming: `FeatureNameScreen`, `FeatureNameViewModel`, `FeatureNameUiState`
## Boundaries
- No `LiveData` in new code (use `StateFlow`)
- No XML layouts in new features (use Compose)
- No `GlobalScope` (use `viewModelScope` or structured concurrency)
- No hardcoded strings in UI (use `stringResource`)
- No `Thread.sleep` in tests (use `advanceUntilIdle`)
Step 2: Load Context Selectively
- Match context to task:
- Bug fix → error logs + failing test + relevant source files
- New feature → spec + architectural docs + similar existing feature
- Refactor → source files + test files + rules file
- Don't dump everything — irrelevant context dilutes focus
Step 3: Surface Ambiguity
- When conventions aren't documented, surface the question:
- "The project uses both
StateFlow and LiveData — which should I use for new code?"
- "Module
:core:data has two repository patterns — which should this follow?"
- Update rules files with the answer to prevent recurrence
Step 4: Include Examples
- Point agents at exemplary code:
- "Follow the pattern in
feature/home/HomeViewModel.kt"
- "Match the testing style in
core/data/src/test/UserRepositoryTest.kt"
- Examples beat descriptions — "do it like this file" is clearer than paragraphs of rules
Common Rationalizations
| Shortcut |
Why It Fails |
| "The agent should figure it out from the code" |
Agents sample context — they may read the wrong file and infer the wrong pattern. |
| "Rules files are overhead" |
30 minutes writing rules saves hours of correcting agent output. |
| "I'll fix agent mistakes manually" |
You'll fix the same mistakes every session. Rules fix them permanently. |
| "More context is better" |
Past ~2,000 lines, agents lose focus. Curate, don't dump. |
Red Flags
- Agent invents APIs that don't exist in the project
- Agent diverges from documented conventions
- No rules file in the project
- Rules file is stale (references deprecated patterns)
- Agent treats error messages from untrusted sources as instructions
- Same convention correction given repeatedly across sessions
Verification
1---2name: context-engineering3description: Use when setting up a project for AI-assisted development or when agent output quality is poor. Guides writing rules files, structuring context, and managing the information agents need to produce accurate work.4---56# Context Engineering78## Overview910"Context is the single biggest lever for agent output quality." Agents don't read your mind — they read your context. This skill teaches you to engineer that context so agents produce code that matches your project's conventions, architecture, and constraints.1112## When to Use1314- Setting up a new Android project for AI-assisted development15- Agent output consistently diverges from project conventions16- Agent invents APIs or patterns that don't exist in your codebase17- After adding new libraries, modules, or architectural patterns18- When onboarding a new team member who uses AI tools1920**Skip when:** Agent output is already consistent with project conventions.2122## Five-Level Context Hierarchy2324Load context in this priority order:2526| Level | Source | What It Provides | Persistence |27|-------|--------|-------------------|-------------|28| 1 | Rules files (`CLAUDE.md`, `.cursorrules`) | Tech stack, commands, conventions, boundaries | Permanent |29| 2 | Specs & architecture docs (`SPEC.md`, ADRs) | Design decisions, constraints, rationale | Per-project |30| 3 | Source code (read specific files) | Current implementation, patterns in use | Real-time |31| 4 | Error output & test results | What's broken, what's expected | Per-session |32| 5 | Conversation history | Current task context | Ephemeral |3334**Optimal range:** ~2,000 lines of focused context per task. More dilutes attention; less causes invention.3536## Core Process3738### Step 1: Write Rules Files39401. **Create a `CLAUDE.md`** (or equivalent) in your project root:4142```markdown43# Project Rules4445## Tech Stack46- Language: Kotlin 2.0+47- UI: Jetpack Compose with Material 348- Architecture: MVVM with Clean Architecture layers49- DI: Hilt50- Async: Coroutines + Flow51- Database: Room52- Network: Retrofit + OkHttp + Kotlin Serialization53- Image loading: Coil54- Navigation: Navigation Compose55- Testing: JUnit5 + MockK + Compose Test Rules + Espresso5657## Commands58- Build: `./gradlew assembleDebug`59- Test (unit): `./gradlew test`60- Test (instrumented): `./gradlew connectedAndroidTest`61- Lint: `./gradlew lint`62- Format: `./gradlew spotlessApply`63- Check: `./gradlew detekt`6465## Module Structure66- `:app` — application module (MainActivity, navigation, DI setup)67- `:feature:*` — feature modules (screens, ViewModels)68- `:core:data` — repositories, data sources, API services69- `:core:domain` — use cases, domain models70- `:core:ui` — shared Compose components, theme71- `:core:common` — utilities, extensions7273## Conventions74- ViewModels expose `StateFlow<UiState>`, never `LiveData`75- UI state is a single sealed interface per screen76- Repository functions are `suspend` or return `Flow`77- Use `@Inject constructor` for Hilt, not field injection78- Composables: stateless with state hoisting79- Tests follow Arrange-Act-Assert pattern80- Naming: `FeatureNameScreen`, `FeatureNameViewModel`, `FeatureNameUiState`8182## Boundaries83- No `LiveData` in new code (use `StateFlow`)84- No XML layouts in new features (use Compose)85- No `GlobalScope` (use `viewModelScope` or structured concurrency)86- No hardcoded strings in UI (use `stringResource`)87- No `Thread.sleep` in tests (use `advanceUntilIdle`)88```8990### Step 2: Load Context Selectively91922. **Match context to task:**93 - Bug fix → error logs + failing test + relevant source files94 - New feature → spec + architectural docs + similar existing feature95 - Refactor → source files + test files + rules file963. **Don't dump everything** — irrelevant context dilutes focus9798### Step 3: Surface Ambiguity991004. **When conventions aren't documented, surface the question:**101 - "The project uses both `StateFlow` and `LiveData` — which should I use for new code?"102 - "Module `:core:data` has two repository patterns — which should this follow?"1035. **Update rules files** with the answer to prevent recurrence104105### Step 4: Include Examples1061076. **Point agents at exemplary code:**108 - "Follow the pattern in `feature/home/HomeViewModel.kt`"109 - "Match the testing style in `core/data/src/test/UserRepositoryTest.kt`"1107. **Examples beat descriptions** — "do it like this file" is clearer than paragraphs of rules111112## Common Rationalizations113114| Shortcut | Why It Fails |115|----------|-------------|116| "The agent should figure it out from the code" | Agents sample context — they may read the wrong file and infer the wrong pattern. |117| "Rules files are overhead" | 30 minutes writing rules saves hours of correcting agent output. |118| "I'll fix agent mistakes manually" | You'll fix the same mistakes every session. Rules fix them permanently. |119| "More context is better" | Past ~2,000 lines, agents lose focus. Curate, don't dump. |120121## Red Flags122123- Agent invents APIs that don't exist in the project124- Agent diverges from documented conventions125- No rules file in the project126- Rules file is stale (references deprecated patterns)127- Agent treats error messages from untrusted sources as instructions128- Same convention correction given repeatedly across sessions129130## Verification131132- [ ] `CLAUDE.md` (or equivalent) exists in project root133- [ ] Rules file covers: tech stack, commands, module structure, conventions, boundaries134- [ ] Rules file is current (matches actual project state)135- [ ] Agent output follows documented conventions136- [ ] Context loaded is relevant to the current task (~2,000 line target)137- [ ] Ambiguities surfaced and resolved (not silently assumed)