The agent invented a test command
You ask your AI agent to add a feature. It writes the code, then runs pytest. But your project uses unittest with a custom runner. The agent gets an error, tries python -m pytest, fails again, and finally invents a directory called tests/ that doesn't match your test/ folder. You spend the next hour fixing its mess.
This is not a prompt problem. It's a project problem. As Real Python's guide on agentic coding notes, an agent's reliability depends heavily on how well the project communicates its conventions. A Python project for agentic coding answers the questions an agent would otherwise guess at: how your project is organized and which tools to run.
The fix is not a longer prompt or a different agent. It's making your project's expectations explicit. In this build-along, we'll start with an ambiguous project, watch an agent guess, then add signals one at a time and see what each one fixes.
Start with an ambiguous project
Create an empty directory and add a single Python file:
# app.py
def add(a, b):
return a + bNo pyproject.toml, no README, no test folder. Now ask your agent (Claude Code, Cursor, or any other) to "add tests for the add function". Here's a hypothetical transcript of what might happen:
Agent: I'll create a test file. Running
pytest... Error:pytest: command not foundAgent: Let me trypython -m pytest... Error:No module named pytestAgent: I'll install pytest and createtests/test_app.py. Agent: Runningpytest tests/... Error:file or directory not found: tests/Agent: I'll create thetests/directory and try again.
The agent guessed the test runner, the test directory, and the installation step. Each guess was reasonable but wrong for your project. The result: wasted time and a structure you didn't want.
Add explicit signals one at a time
Real Python's tutorial builds a project from an empty directory, adding configuration files, tests, linters, and documentation. Each addition answers a question the agent would otherwise guess. Let's do the same, but focus on what each signal fixes. (We'll also add a CI step, which goes beyond the source tutorial.)
Signal 1: pyproject.toml declares your tools
A pyproject.toml file tells agents and humans which tools to run and how. Here's a minimal but complete example:
[build-system]
requires = ["setuptools>=61"]
build-backend = "setuptools.build_meta"
[project]
name = "my-project"
version = "0.1.0"
description = "A sample project for agentic coding"
requires-python = ">=3.9"
dependencies = []
[project.optional-dependencies]
dev = [
"pytest>=7.0",
"ruff>=0.1.0",
"mypy>=1.0",
]
[tool.pytest.ini_options]
testpaths = ["tests"]
[tool.ruff]
line-length = 88
[tool.mypy]
strict = trueThis file answers: which test runner (pytest), which linter (ruff), which type checker (mypy), and where tests live (tests/). This gives the agent an explicit signal for the test runner and test location, reducing the chance it guesses wrong. It also provides a documented install command (pip install -e ".[dev]") that the agent can follow.
What it fixes: Reduces guessing about test commands and directories. It also gives the agent a documented install command to follow.
Signal 2: a tests directory with one example
Create a tests/ directory with a single test file:
# tests/test_app.py
from app import add
def test_add():
assert add(2, 3) == 5This gives the agent a pattern to follow. It sees the import style, the test naming convention, and the assertion style. Without it, the agent might create test_app.py at the root or use a different framework.
What it fixes: Reduces guessing about where tests go and how they're written. The agent can copy the existing pattern.
Signal 3: a README with commands
A short README that lists the exact commands for setup, test, lint, and type-check removes ambiguity:
# My Project
## Setup
pip install -e ".[dev]"
## Test
pytest
## Lint
ruff check .
## Type check
mypy .A README is a common place agents look for commands. Documenting commands in a README gives the agent a reference it can follow. This is a convention, not a guarantee.
What it fixes: Reduces the chance the agent tries an unlisted tool like flake8.
Signal 4: a linter and type checker in CI
Add a CI workflow that runs the same commands. This creates a feedback loop: the agent can run the commands locally, but CI catches mistakes before merge. Real Python notes that automated tests, linters, type checkers, configuration files, and concise documentation create feedback loops that help both humans and AI catch mistakes early.
Here's a minimal GitHub Actions workflow that runs the same commands:
# .github/workflows/ci.yml
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- run: pip install -e ".[dev]"
- run: pytest
- run: ruff check .
- run: mypy .What it fixes: CI runs the same commands you documented. If the agent's output ignores the configuration, CI fails and a human (or the agent, if it checks CI results) can correct it. This does not imply automatic agent correction.
Before and after: a hypothetical agent transcript
Let's compare the same request before and after these signals. The request: "Add a function to multiply two numbers and test it." These transcripts are illustrative, not real logs.
Before (ambiguous project):
Agent: I'll create
multiply.pyandtest_multiply.py. Agent: Runningpython -m unittest... Error:Ran 0 tests(test file not discoverable) Agent: I'll install pytest and runpytest. Error:No tests foundAgent: I'll move the test totests/and try again.
After (explicit project):
Agent: I'll add
multiplytoapp.pyand a test totests/test_app.py. Agent: Runningpytest... Success: 2 passed. Agent: Runningruff check .... Success: No issues. Agent: Runningmypy .... Success: No issues.
The difference is not the agent's intelligence. It's the project's explicitness.
Tradeoffs: explicitness costs maintenance
Making everything explicit has a cost. Every configuration file, README section, and CI step is something you must maintain. If you change your test runner, you must update pyproject.toml, the README, and CI. That's three places instead of one.
There's also a risk of over-specification. If you declare every possible detail, you box the agent in. It can't make reasonable choices about file names, function signatures, or minor refactors. Real Python's guide focuses on the questions an agent would otherwise guess at: project organization and tools. It doesn't recommend specifying every line of code.
A simple rule: declare anything that affects correctness or reproducibility. Leave creative choices to the agent. For example:
Declare โ Leave to the agent ยท Test runner and test directory โ Test function names ยท Linter and type checker commands โ Minor formatting decisions ยท Dependency installation command โ Order of imports ยท CI steps โ Internal helper function names
This table is a guideline, not a law. The key is to ask: would a wrong guess here cause a failure that's hard to debug? If yes, declare it.
A rule for deciding what to declare
Use this two-question test for any project detail:
- Would the agent have to guess this to complete a task?
- Would a wrong guess cause an error, a failed test, or a structure you don't want?
If both answers are yes, declare it explicitly. If either is no, leave it to the agent.
For example, the test command: the agent must guess it to run tests (yes), and a wrong guess causes an error (yes). Declare it. The name of a new helper function: the agent must choose it (yes), but a wrong guess is easily fixed and doesn't break anything (no). Leave it.
This rule keeps your configuration lean. You add signals only where they prevent real problems.
What this doesn't fix
Explicit project structure reduces guessing, but it doesn't make agents infallible. Agents can still misinterpret requirements, write incorrect logic, or ignore configuration. Real Python's guide presents this as a way to make workflows more predictable, not as a guarantee.
Also, the evidence doesn't specify how different agents (Claude Code, Cursor, etc.) handle pyproject.toml or READMEs. The guide is agent-agnostic. For provider-specific behavior, check your agent's documentation. The generic principle is: agents read the files you give them, so make those files clear.
Finally, this approach assumes your project has a single, consistent structure. If you have multiple services or monorepos, you may need per-service configuration. The evidence doesn't cover that case, so treat it as an extension.
Where to go next
Once your project is explicit, you can focus on higher-level agent reliability. For example, testing what your LLM agent actually did in CI covers how to verify agent actions, and engineering practices for long-horizon coding agents discusses planning and review. These build on the foundation of an explicit project.
The core idea is simple: agents guess when your project is ambiguous. Make it explicit, and they stop guessing. Start with pyproject.toml, add a test example, write a short README, and wire up CI. Each signal removes a guess.
Further reading
- The test passed. You still don't know what happened.
- Agentic Engineering: Planning, Review and Production Triage