If you’ve watched GitHub Trending over the past month, one shift is impossible to ignore: AI coding has moved from simple code completion to autonomous terminal agents like Aider, Cline, and Claude Code.
However, many developers still struggle with context degradation, hallucinations, and code regressions when pair-programming with LLMs.
Here are 5 battle-tested AI coding patterns extracted from the architecture of these top-trending GitHub tools that you can apply directly to your daily workflow.
1. Stop Pasting Entire Files: Embrace "Repo Map" Pruning
One of the primary causes of LLM confusion and context bloat is dumping entire files into the prompt.
Tools like Aider solve this using Tree-Sitter syntax maps:
- Instead of the full 500-line implementation, feed the AI only the file's type definitions, class interfaces, and exported function signatures.
- The LLM understands the exact architectural surface without consuming thousands of precious context tokens.
// ✅ Good context: Provide the interface seam, not the internal wiring
export interface CacheAdapter {
get<T>(key: string): Promise<T | null>;
set<T>(key: string, value: T, ttlSeconds?: number): Promise<void>;
invalidate(pattern: string): Promise<number>;
}
// The model knows how to wire against CacheAdapter without seeing its 300 lines of Redis logic!
Pro Tip: When asking an LLM to implement a new feature, provide the interfaces of the dependencies and only the exact file being modified.
2. The Spec-First TDD Loop: Never Let the AI Test Its Own Code Blindly
The most common trap: asking an LLM to write a function and a test in the same prompt. The model will often write broken logic and a compliant, tautological test that passes regardless of bugs.
Instead, enforce a strict 3-turn TDD loop:
- Turn 1 (The Contract): Ask the AI to write a strict specification or interface.
-
Turn 2 (Red Test): Instruct the AI to write a unit test based strictly on that contract, and run it locally to verify it fails (
Red). -
Turn 3 (Green Implementation): Feed the test failure output to the AI and instruct it to write the minimal code needed to pass (
Green).
# Don't ask the AI if it works. Let your terminal prove it:
$ npm test -- tests/auth.spec.ts
# Copy the exact stack trace back to the LLM:
# "Received 401 instead of 403 on invalid token. Fix src/auth.ts to satisfy the test."
3. Treat git diff -U0 as the Ultimate Hallucination Guardrail
Autonomous agents often suffer from "stealth edits"—unintentionally deleting comments, tweaking unrelated imports, or altering styling conventions.
Before accepting any AI-generated modification, run a unified diff check with zero context lines:
git diff -U0 | grep '^-'
- Inspect every deleted line (
-). - Did the AI delete an important docstring?
- Did it rewrite an existing helper instead of reusing it?
- Rule: If a change isn't directly related to your prompt, reject it immediately or prompt: "Revert changes to lines 45-60 and preserve the original docstrings."
4. Split Monolithic Instructions into Scoped Rule Files
Dumping a 2,000-word .cursorrules or prompt file slows down inference and causes instruction drift (the model ignores rules placed in the middle).
Leading repositories now advocate for Glob-Scoped Rule Sets (similar to ESLint configs):
- Keep your root instructions under 20 lines (core tech stack and testing commands).
- Create domain-specific rule files triggered only when relevant files are touched:
-
rules/backend.md: Triggered only forsrc/api/**/*.ts(DB transaction rules, error codes). -
rules/frontend.md: Triggered only forsrc/components/**/*.vue(accessibility, styling tokens).
-
This keeps active context clean, minimizes token spend, and boosts instruction adherence significantly.
5. Give Your Agent Read-Only Verification Seams via MCP
Prompting an LLM: "Here is my database schema, please write the migration" often leads to slight column name mismatches or hallucinated foreign keys.
With the rise of the Model Context Protocol (MCP), the best practice is giving the agent read-only inspection tools:
- An MCP database tool that runs
DESCRIBE table;directly against your local dev database. - An MCP filesystem tool that reads live directory structures.
When the agent can query runtime reality instead of relying on memory, hallucination rates on complex refactors drop to near zero.
Summary Checklist for Your Next AI Coding Session
- [ ] Context: Am I providing interfaces/signatures instead of full implementation files?
- [ ] Verification: Did I write a failing test before generating production code?
- [ ] Audit: Did I run
git diffto ensure no collateral code/comment damage? - [ ] Scope: Are my system instructions modular and scoped to the task at hand?
What patterns have worked best in your AI-assisted workflow? Drop your tips in the comments below! 👇
Top comments (0)