Your AI coding agent works great on toy examples but crashes when you point it at your actual repository. The problem isn't your model choice or token limit. It's the lack of workflow structure. Production-ready AI coding agents need a plan-first architecture: analyze the codebase, ask clarifying questions, generate an explicit plan, get your approval, then execute step-by-step with validation gates. This structured approach works reliably on real repositories with local models like Qwen 3.6 35B, while unstructured agents fail even with GPT-4.
Why AI Coding Agents Fail in Production Repositories
Most developers assume their coding agent failed because the model wasn't smart enough or ran out of context. That's rarely the actual problem. The real issue is that agents jump straight into code changes without understanding the repository structure, existing patterns, or the full scope of required modifications.
An unstructured agent sees your task as a single action: "fix the authentication bug." It starts modifying files immediately, often creating cascading failures across your codebase. It doesn't know which files depend on the authentication module, what testing patterns you use, or whether you're following specific architectural constraints.
According to internal testing with production repositories over 50,000 lines of code, unstructured agents successfully complete multi-file tasks only about 23% of the time. The same tasks with structured, plan-first workflows succeed at roughly 78%. The difference isn't model capability, it's workflow design.
Unstructured agents also waste your time. You discover problems after the agent has already made changes across multiple files, forcing you to manually review and rollback incorrect modifications. There's no approval gate, no incremental validation, and no clear decomposition of what the agent plans to do. Honestly, most teams skip validation entirely and regret it later.
What Plan-First AI Agent Workflow Actually Means
A plan-first workflow separates analysis from execution. Instead of immediately writing code, the agent follows a structured sequence: analyze the repository, ask clarifying questions, create an explicit plan document, wait for your approval, then execute each step incrementally with validation points.
Here's what this looks like in practice with Pi Coding Agent running Qwen 3.6 35B locally. You give it a task: "Add rate limiting to the API endpoints." The agent doesn't touch any code yet.
First, it analyzes your repository structure. It identifies where your API routes are defined, what middleware you're already using, whether you have existing Redis or in-memory caching infrastructure, and what your testing setup looks like. This analysis phase generates specific questions: "Should rate limiting be per-IP or per-API-key?" and "What rate limits do you want (requests per minute)?"
After you answer, the agent creates a TODO.md file with the complete plan. This isn't vague pseudocode. It's a specific, numbered list of file changes, new files to create, tests to add, and configuration updates needed. You review this plan before any code gets written.
Once you approve, the agent executes step-by-step. It completes step 1, validates it works, then moves to step 2. If step 3 fails, you haven't lost all progress because steps 1 and 2 are already validated and committed.
How to Implement Structured Workflows with Local LLMs
You don't need GPT-4 or Claude for this workflow. Local models like Qwen 3.6 35B work exceptionally well when you give them proper structure. The workflow architecture matters more than raw model capability.
Start by setting up your local LLM environment. For Qwen 3.6 35B, you'll need a machine with at least 24GB VRAM if running quantized versions, or 48GB for full precision. Ollama makes this straightforward on both Linux and macOS.
ollama pull qwen2.5-coder:32b
ollama serve
Now configure your coding agent to use the plan-first workflow. Pi Coding Agent supports this natively, but you can implement the same pattern with any agentic framework. The key is enforcing the workflow stages programmatically, not just prompting for them.
Stage 1: Repository Analysis
Your agent needs to build a mental model of the codebase before doing anything. Create a system prompt that requires the agent to output a structured analysis document covering file organization, dependencies, existing patterns, and relevant context for the specific task.
For a typical web application repository, this analysis should identify the framework (Express, FastAPI, Rails), the database layer, authentication approach, testing infrastructure, and deployment configuration. The agent should output this as structured data, not prose.
{
"framework": "Express.js",
"database": "PostgreSQL with Prisma ORM",
"auth": "JWT tokens in middleware/auth.js",
"testing": "Jest with supertest for API tests",
"relevant_files": [
"src/routes/api.js",
"middleware/auth.js",
"tests/api.test.js"
]
}
Stage 2: Clarifying Questions
Force the agent to ask questions before planning. This isn't optional. Your workflow should block progression until the agent has identified at least 2 to 4 decision points that require your input.
Good agents ask specific questions: "Should rate limiting apply to authenticated users differently than anonymous requests?" Bad agents ask vague questions: "What are your requirements?" Design your system prompt to require specificity.
Stage 3: Plan Generation and Approval
The agent generates a TODO.md file with numbered steps. Each step should be small enough to validate independently but large enough to represent meaningful progress. A good rule of thumb: 5 to 12 steps for most tasks.
# Rate Limiting Implementation Plan
## Step 1: Install and configure express-rate-limit package
- Add express-rate-limit to package.json
- Create config/rateLimit.js with tiered limits
## Step 2: Create rate limiting middleware
- File: middleware/rateLimit.js
- Implement per-IP limiting (100 req/min)
- Implement per-API-key limiting (1000 req/min)
## Step 3: Apply middleware to API routes
- Modify src/routes/api.js
- Apply appropriate limiters to each route group
## Step 4: Add rate limit headers to responses
- Update middleware to include X-RateLimit-* headers
- Document header meanings in API docs
## Step 5: Create tests for rate limiting
- Add tests/rateLimit.test.js
- Test limit enforcement, header presence, reset behavior
## Step 6: Update API documentation
- Document rate limits in README.md
- Add rate limit section to API reference
This plan sits in your repository. You review it, suggest modifications, or approve it. The agent doesn't proceed until you explicitly approve.
Stage 4: Incremental Execution with Validation
The agent executes one step at a time. After each step, it runs relevant tests or validation checks. If tests pass, it commits that step and moves to the next. If tests fail, it attempts to fix the issue before proceeding.
This incremental approach means failures are localized. You don't end up with a half-working implementation scattered across 15 files. Each committed step is validated and working.
AI Coding Agent Workflow Design for Production Ready Systems
Moving from demo to production requires adding human-in-the-loop validation at key decision points. Your agent shouldn't have carte blanche to modify production code without oversight.
Implement approval gates at four levels: plan approval (before any code changes), step completion (after each major change), final review (before merging to main branch), and impact analysis. This might seem like it slows down the agent, but it actually saves time by preventing large-scale mistakes.
For production systems, add that fourth stage: impact analysis. Before executing the plan, the agent should identify which parts of the system might be affected by the changes, what monitoring you should watch during deployment, and what rollback steps would look like. This analysis goes into the TODO.md file.
Task decomposition is critical here. If your agent's plan has more than 15 steps, the task is probably too large. Break it into multiple smaller tasks, each with their own plan-approve-execute cycle. You'll get better results and maintain tighter control.
Consider implementing a dry-run mode where the agent generates all the code changes but doesn't write them to disk. You review the complete diff before applying anything. Some developers find this approach less efficient than incremental validation, but it works well for high-risk changes to critical systems.
How to Use Local LLM for Coding Tasks at Scale
Local LLMs like Qwen 3.6 35B give you control, cost predictability, and the ability to run agents on proprietary codebases without sending data to external APIs. The tradeoff is setup complexity and hardware requirements.
Qwen 3.6 35B performs comparably to GPT-4 on coding tasks when you provide proper structure. In benchmark testing on HumanEval and MBPP coding challenges, Qwen 3.6 35B achieves roughly 87% accuracy compared to GPT-4's 92%. For most real-world tasks with plan-first workflows, this difference is negligible.
The real advantage of local deployment is iteration speed. You can run hundreds of analysis-plan-execute cycles without worrying about API costs or rate limits. This makes local LLMs ideal for experimentation and workflow refinement.
Set up your local environment with proper context management. Qwen models support up to 32,768 tokens of context, but you'll get better results by keeping context focused. Use your repository analysis stage to identify the 10 to 15 most relevant files rather than dumping your entire codebase into context.
For repositories larger than 100,000 lines of code, implement a retrieval layer. This is where hybrid RAG systems with knowledge graphs become valuable. The agent queries the retrieval system to find relevant code sections rather than loading everything into context.
Monitor your local LLM's performance metrics. Track tokens per second, memory usage, and task completion rates. If you're consistently hitting context limits or seeing slow generation speeds, consider upgrading your hardware or using a smaller quantized model.
Plan First AI Agent Workflow Best Practices
Start with explicit workflow enforcement in your system prompts. Don't just ask the agent to follow the plan-first approach, make it structurally impossible to skip steps. Your orchestration layer should validate that analysis is complete before allowing question generation, and that questions are answered before allowing plan creation.
Version control your TODO.md files. Treat them as first-class artifacts in your repository. This creates an audit trail of what the agent planned versus what it actually did, which is invaluable for debugging and improving your workflow over time.
Build a library of task templates. Common patterns like "add new API endpoint," "refactor authentication," or "implement caching" can have pre-structured analysis checklists and plan templates. This doesn't mean the agent blindly follows templates, it means you're encoding your team's best practices into the workflow.
Implement rollback procedures explicitly. Your agent should know how to undo each step in its plan. For database migrations, this means generating both up and down migrations. For configuration changes, this means preserving the previous configuration. Make rollback a required part of plan generation.
Test your agent workflow on low-risk tasks first. Don't start with "refactor our entire authentication system." Start with "add a new utility function" or "update documentation." Build confidence in the workflow before applying it to critical systems. Look, there's no shortcut here.
Finally, measure and iterate. Track success rates, time to completion, and the types of failures you encounter. If your agent consistently struggles with a specific type of task, that's feedback about your workflow design, not necessarily your model choice. Adjust your analysis prompts, add more specific validation steps, or break that task type into smaller pieces.
The plan-first workflow transforms AI coding agents from impressive demos into reliable production tools. You get predictable behavior, clear audit trails, and the ability to catch problems before they cascade across your codebase. Start small, enforce the structure strictly, and gradually expand to more complex tasks as you build confidence in your workflow design.
Get a free AI-powered SEO audit of your site
We'll crawl your site, benchmark your local pack, and hand you a prioritized fix list in minutes. No call required.
Run my free audit