Why Your AI Agent Writes Too Much Code—and How to Fix It
I’m Denis Shokhirev, Enterprise AI architect based in Erlangen, Germany, and founder of DennisCraft AI Studio. Over the last six months, I shipped 14 production AI agents for DACH B2B clients—logistics, fintech, industrial automation—using a stack of Claude, Supabase, n8n, Doppler, and self-hosted Postgres. Again and again, I saw the same issue: LLM-powered agents generate far more code than your business logic actually needs. Here’s how this happens, why it’s dangerous, and how I’ve built proce
I’m Denis Shokhirev, Enterprise AI architect based in Erlangen, Germany, and founder of DennisCraft AI Studio. Over the last six months, I shipped 14 production AI agents for DACH B2B clients—logistics, fintech, industrial automation—using a stack of Claude, Supabase, n8n, Doppler, and self-hosted Postgres. Again and again, I saw the same issue: LLM-powered agents generate far more code than your business logic actually needs. Here’s how this happens, why it’s dangerous, and how I’ve built process to force concise, production-grade output.
Why LLM Agents Bloat Code by Default
Modern LLMs (Claude, GPT-4, Gemini) are trained on vast, noisy codebases—GitHub, Stack Overflow, documentation dumps. Their sampling preference: “more is safer.” They’d rather over-specify, producing extra scaffolding, helpers, and abstraction layers—most of which your system will never call.
Real-World Example
Recently, for a fintech client, a Claude-based agent generated 3,200+ lines of Python for an ETL task that should have been a 200-line ORM wrapper. Five unused helper files, three “manager” classes, and dozens of dead functions—all shipped in a PR. The redundancy was obvious at review but would’ve slipped through in a high-velocity pipeline.
Risks of Excess AI-Generated Code
| Risk | Description | Impact |
|---|---|---|
| Attack Surface | More code means more opportunities for vulnerabilities | Higher false positives in bandit/semgrep, more SQLi/XSS risk |
| Review Overhead | Code reviews slow down with bloated diffs | QA/devops take longer, releases slip |
| Maintenance Debt | Unused code breaks during refactors | Production bugs, unexplained behavior |
A 2023 Stanford CodeGen study found 38% of LLM-generated CRUD code contained CWE-89 (SQL injection) patterns (arxiv.org/abs/2307.07924). In my deployments, every time code size exceeded 1,000 lines for a CRUD service, static analysis flagged at least 2–3 critical issues missed by initial review.
Root Causes: Why LLMs Overproduce Code
1. “Safer to Overproduce” Bias
LLMs optimize for “completeness” as judged by their training data: better to overdeliver than risk missing edge cases. This leads to scaffolding, boilerplate, and “helper” code that may never be used.
2. Prompts Without Hard Constraints
If you ask Claude or GPT-4 to “write production-ready code” with no explicit length or file structure limits, they’ll flood you with “everything you might need.” Without constraints, code size balloons.
3. No Automated Post-Processing
Many teams go straight from LLM → PR. There’s no code trimming, dead code elimination, or static checks before human review. This doesn’t scale—manual clean-up is slow and error-prone.
Practical Patterns to Force Concise, Safe Output
1. Prompt with Strict Limits
Every prompt to Claude or GPT-4 should specify max file count, line count, and require single-file output when possible. Ask for one-line docstrings, and force business logic to be described in comments.
PROMPT = """
Write a Python class for connecting to Supabase.
Single file, max 80 lines.
Document only public methods.
No tests or extra dependencies.
"""2. Automated Static Analysis and Trimming
Immediately run semgrep and bandit on generated code. Use Python scripts to detect and remove unused functions or imports before PR. This kills 50–80% of bloat in practice.
import ast
def remove_unused_functions(code):
tree = ast.parse(code)
used = {node.id for node in ast.walk(tree) if isinstance(node, ast.Name)}
new_body = []
for node in tree.body:
if isinstance(node, ast.FunctionDef):
if node.name in used:
new_body.append(node)
else:
new_body.append(node)
tree.body = new_body
return ast.unparse(tree)
3. n8n Pipeline for Code QA
I build n8n flows that, after LLM output, run static checks (semgrep, bandit, gitleaks), then route code to a reviewer. Only after human sign-off does code hit main. This is enforceable, repeatable, and works at scale.
- name: LLM Code QA
steps:
- run: semgrep --config=auto src/
- run: bandit -r src/
- run: gitleaks detect --source=src/
- assign: code-reviewer
- if: passed
then: merge
Tool Comparison: Static Analysis for LLM Output
| Tool | Languages | Focus | Pipeline Integration |
|---|---|---|---|
| semgrep | Python, JS, TS, Go, etc. | Pattern finding, dead code | CLI, n8n, CI/CD |
| bandit | Python | Security, CWE coverage | CLI, CI |
| gitleaks | Any | Secrets detection | CLI, n8n |
FAQ
Why not just trim LLM code manually?
Manual edits don’t scale and miss subtle dead code. Automated static analysis and code trimming save hours per release and catch more issues.
Do ORMs/frameworks help reduce boilerplate?
ORMs help, but LLMs still add layers unless you prompt for minimal implementation and run static checks after. Explicit prompts and post-processing are both required.
Is auto-trimming totally reliable?
No—always keep a human review step, especially for production or regulated environments. But auto-trimming eliminates most obvious bloat safely.
How does this actually improve security?
Less code, less attack surface. Tools like bandit and semgrep catch many critical bugs before they reach production, especially in LLM-generated code.
How can I add this to my current pipeline?
Build static checks and code trimming into n8n or your CI/CD toolchain, and enforce prompt constraints up front. Roll out gradually—one agent at a time if needed.
At which pipeline stage do you catch the most excess or unsafe LLM-generated code—prompting, static analysis, or human review? I’d genuinely like to know. I run a free 30-min stack audit for DACH founders building AI in regulated markets. DM me on LinkedIn or write to @ger_dennis_ai.
Turn your process into an AI system
Fixed price. Production quality. DACH B2B focus.