Avoid Context Poisoning

Context poisoning is one of the most critical issues that can affect the quality and reliability of AI-assisted development.

Understanding how to identify, prevent, and mitigate context poisoning is essential for maintaining productive workflows with the Gamecraft Agent and ensuring code quality in your Unity projects.

What is Context Poisoning?

Definition

Context poisoning occurs when inaccurate, misleading, or irrelevant information enters the language model's active context window. This contaminated information then influences all subsequent responses, causing the AI to:

  • Draw incorrect conclusions about your project

  • Provide erroneous information to development tools

  • Make assumptions based on false premises

  • Progressively deviate from your intended goals

  • Generate code that doesn't match your actual project structure

How It Spreads

Context poisoning is particularly insidious because it compounds over time:

  1. Initial Contamination: Incorrect information enters the context

  2. False Assumptions: The AI builds upon the incorrect information

  3. Cascading Errors: Each response reinforces and expands the misinformation

  4. Tool Pollution: Incorrect assumptions affect tool usage and file operations

  5. Progressive Deviation: The conversation drifts further from reality with each exchange

Real-World Example

❌ Poisoned Context Flow:
User: "Fix the bug in PlayerMovement.cs"
AI: [Assumes PlayerMovement.cs exists, analyzes wrong file]
AI: "I've optimized your PlayerMovement script..."
User: "Now update the related enemy AI"
AI: [Builds on false PlayerMovement assumptions]
AI: "Based on your PlayerMovement patterns, I'll update EnemyAI..."
[Each step gets further from reality]

Common Sources of Context Poisoning

1. Outdated Project Information

Problem: The AI references old project states or deleted files

❌ "I see you're using the old Animation system in PlayerController.cs"
✅ "Let me check your current PlayerController implementation first"

Prevention:

  • Regularly verify file existence before making assumptions

  • Ask the AI to confirm current project state

  • Use file search tools to validate references

2. Incorrect File Paths and Names

Problem: Typos or misremembered file locations poison the context

❌ User: "Update Scripts/Player/PlayerController.cs"
❌ AI: [Operates on wrong file or creates incorrect file]
✅ User: "Update the PlayerController script in the Scripts folder"
✅ AI: [Searches for correct file first]

Prevention:

  • Use file search tools to find exact paths

  • Copy-paste file paths from the explorer when possible

  • Let the AI verify file locations before proceeding

3. Assumption-Based Responses

Problem: The AI makes educated guesses instead of gathering facts

❌ "Based on typical Unity projects, your scripts are probably in Assets/Scripts/"
✅ "Let me search for your script files to understand your project structure"

Prevention:

  • Encourage fact-checking before assumptions

  • Request explicit verification of project details

  • Use semantic search to understand actual project structure

4. Copy-Paste Errors from External Sources

Problem: Code or information copied from tutorials or documentation doesn't match your project

❌ Pasting Unity 2019 tutorial code into Unity 2023 project
❌ Using deprecated API examples
❌ Applying mobile-specific solutions to desktop projects

Prevention:

  • Specify your Unity version and target platform

  • Verify API compatibility before implementing suggestions

  • Test small changes before large refactors

5. Misunderstood Requirements

Problem: Initial misunderstandings propagate through the entire conversation

❌ User: "I need a save system"
❌ AI: [Assumes JSON file saves]
❌ AI: [Builds JSON-based implementation]
❌ User: "Now add cloud sync"
❌ AI: [Tries to add cloud sync to JSON system instead of suggesting proper solution]

Prevention:

  • Be explicit about requirements and constraints

  • Correct misunderstandings immediately

  • Restart conversations when fundamental assumptions are wrong

Identifying Context Poisoning

Warning Signs

Watch for these indicators that context may be poisoned:

1. File Operation Errors

❌ "Cannot find file: Assets/Scripts/NonExistentScript.cs"
❌ "Permission denied when writing to read-only file"
❌ "Invalid file path format"

2. Inconsistent Assumptions

❌ AI refers to features you never implemented
❌ AI mentions classes or systems that don't exist
❌ AI assumes different Unity version than you're using

3. Increasingly Irrelevant Suggestions

❌ AI suggests VR-specific solutions for 2D mobile game
❌ AI recommends enterprise-scale patterns for indie project
❌ AI focuses on performance optimizations when you need basic functionality

4. Tool Misuse

❌ AI tries to edit files that don't exist
❌ AI runs commands for wrong operating system
❌ AI uses deprecated Unity APIs or methods

5. Circular Reasoning

❌ AI: "As we established earlier, your project uses System X"
❌ [But System X was never confirmed to exist]
❌ AI: "Building on our System X implementation..."

Diagnostic Prompts

Use these prompts to check for context poisoning:

"Before proceeding, please verify:
1. What Unity version do you think I'm using?
2. What platform am I targeting?
3. What file structure do you believe my project has?
4. What systems have we actually implemented together?"
"List the files we've discussed in this conversation and confirm which ones actually exist in my project."
"What assumptions are you making about my project that we haven't explicitly verified?"

Prevention Strategies

1. Context Hygiene

Start Fresh When Needed:

✅ Begin new conversations for unrelated tasks
✅ Clear context when switching between different projects
✅ Restart when fundamental assumptions prove wrong

Verify Before Building:

✅ "Please search for existing PlayerController implementations before creating a new one"
✅ "Check what input system I'm currently using before suggesting improvements"
✅ "Verify my current Unity version and target platform"

2. Explicit Project State

Provide Clear Context:

✅ "I'm using Unity 2023.2, targeting iOS/Android, using the new Input System"
✅ "My project structure: Assets/Scripts/Player/, Assets/Scripts/Enemies/, Assets/Scripts/UI/"
✅ "Current systems implemented: Movement, Basic Combat, UI Framework"

Regular State Updates:

✅ "We've now implemented the inventory system, so update your understanding of the project"
✅ "I've switched from JSON saves to binary saves - please note this change"
✅ "Target platform changed from mobile to PC - adjust recommendations accordingly"

3. Fact-Based Workflows

Evidence-Driven Development:

✅ Use file search before making changes
✅ Read existing code before suggesting modifications
✅ Verify API compatibility before implementation
✅ Test assumptions with small experiments

Documentation Integration:

✅ Reference official Unity documentation
✅ Check current API references
✅ Verify best practices for your Unity version
✅ Cross-reference with platform-specific guidelines

Mitigation Techniques

1. Context Reset

When to Reset:

  • Fundamental assumptions prove incorrect

  • File structure misunderstandings compound

  • Target platform or Unity version confusion

  • Multiple failed attempts based on wrong information

How to Reset:

"I need to reset our conversation context. Here's the current state of my project:
- Unity Version: 2023.2.1f1
- Target Platform: Mobile (iOS/Android)
- Current Systems: [List actual implemented systems]
- Project Structure: [Actual folder organization]
- Immediate Goal: [Specific current objective]"

2. Incremental Verification

Step-by-Step Validation:

✅ "First, confirm the file exists"
✅ "Then, read the current implementation"
✅ "Next, identify the specific issue"
✅ "Finally, implement the targeted fix"

Checkpoint Validation:

✅ "Before we continue, let's verify this change worked as expected"
✅ "Please confirm the modified code compiles without errors"
✅ "Test this small change before proceeding to the next step"

3. Context Segmentation

Isolate Different Concerns:

✅ Separate conversations for different systems
✅ New context for different project phases
✅ Clean slate for troubleshooting vs. new development
✅ Different sessions for different team members' work

Clear Boundaries:

✅ "This conversation is only about the UI system"
✅ "We're focusing solely on performance optimization today"
✅ "This session is for bug fixing, not new features"

Recovery Strategies

1. Immediate Correction

Quick Fixes:

"Stop - that assumption is incorrect. Let me clarify:
[Provide correct information]
Please adjust your understanding and try again."

Explicit Corrections:

"I need to correct something from earlier:
- We don't actually have a PlayerHealth system yet
- The file path is Assets/Player/, not Assets/Scripts/Player/
- I'm using the old Input Manager, not the new Input System"

2. Gradual Realignment

Systematic Correction:

1. Identify all poisoned assumptions
2. Correct them one by one
3. Verify each correction is understood
4. Rebuild context with accurate information

Evidence-Based Rebuilding:

✅ Use file searches to establish actual project structure
✅ Read existing code to understand current implementations
✅ Verify Unity version and settings
✅ Confirm target platform and constraints

3. Context Reconstruction

When to Reconstruct:

  • Multiple systems are misunderstood

  • File structure is completely wrong

  • Platform assumptions are incorrect

  • API usage is inappropriate for your Unity version

Reconstruction Process:

"Let's rebuild our understanding of this project from scratch:

1. Project Overview:
   - Unity Version: [Actual version]
   - Target Platform: [Actual platform]
   - Project Type: [2D/3D, genre, etc.]

2. Current Architecture:
   - [Actual folder structure]
   - [Implemented systems]
   - [Key classes and their relationships]

3. Immediate Goals:
   - [What we're trying to accomplish]
   - [Specific constraints and requirements]
   - [Success criteria]
"

Unity-Specific Context Poisoning

Common Unity Misconceptions

Version-Specific Issues:

❌ Assuming Legacy Input when project uses Input System
❌ Suggesting deprecated Networking APIs
❌ Using old UI system (UGUI) patterns inappropriately
❌ Mixing Universal Render Pipeline with Built-in Pipeline advice

Platform Confusion:

❌ Mobile-specific solutions for desktop games
❌ Console-specific patterns for mobile targets
❌ WebGL limitations applied to standalone builds
❌ VR/AR assumptions for traditional games

Architecture Misunderstandings:

❌ Assuming MonoBehaviour when ScriptableObject is better
❌ Suggesting Singleton patterns inappropriately
❌ Misunderstanding Component vs. Entity-Component patterns
❌ Incorrect assumptions about Unity's execution order

Unity-Specific Prevention

Version Verification:

"Please confirm my Unity version and recommend solutions compatible with:
- Unity 2023.2.1f1
- Universal Render Pipeline
- New Input System
- Mobile target platform"

Architecture Clarification:

"My project uses:
- Component-based architecture (not ECS)
- ScriptableObject-based data management
- Event-driven UI updates
- Object pooling for performance"

Platform-Specific Context:

"Target platform considerations:
- Mobile: iOS/Android
- Performance: 60fps on mid-range devices
- Memory: <500MB total
- Input: Touch with gesture support"

Best Practices Summary

Prevention Checklist

  • Verify First: Always confirm file existence and project structure

  • Be Explicit: State Unity version, platform, and constraints clearly

  • Check Assumptions: Question and verify AI assumptions regularly

  • Use Evidence: Rely on file searches and code reading over assumptions

  • Start Fresh: Begin new conversations for unrelated tasks

  • Correct Immediately: Fix misunderstandings as soon as they appear

Detection Checklist

  • ⚠️ File Errors: AI tries to access non-existent files

  • ⚠️ Version Mismatches: Suggestions incompatible with your Unity version

  • ⚠️ Platform Conflicts: Advice inappropriate for your target platform

  • ⚠️ Architecture Confusion: Patterns that don't match your project structure

  • ⚠️ Circular References: AI building on unverified assumptions

  • ⚠️ Irrelevant Suggestions: Solutions that don't address your actual needs

Recovery Checklist

  • 🔄 Stop and Correct: Halt progress when you notice poisoning

  • 🔄 Provide Facts: Replace assumptions with verified information

  • 🔄 Reset if Needed: Start fresh when corruption is widespread

  • 🔄 Verify Understanding: Confirm AI has updated its mental model

  • 🔄 Test Incrementally: Validate small changes before proceeding

  • 🔄 Document State: Clearly establish current project reality

Conclusion

Context poisoning is a manageable challenge that becomes easier to handle with experience. The key is developing awareness of how it happens, vigilance in detecting early warning signs, and decisive action when correction is needed.

By maintaining good context hygiene, verifying assumptions, and correcting misinformation promptly, you can ensure that your collaboration with the Gamecraft Agent remains productive and accurate throughout your Unity development journey.

Remember: It's always better to spend a few moments clarifying the current state than to build elaborate solutions on false foundations.

Last updated