5 min read
Keeping your agents in lane
LLMs are non-deterministic. The system you build around them does not have to be.

LLMs are non-deterministic but the system you build around them doesn’t have to be. One of the things people often say about AI agents and LLMs is that they aren’t deterministic so we can’t trust them (i.e. there is a trust issue). Yes, LLMs are unpredictable but that doesn’t mean we can’t build reliable systems around them. The trick is to design the system around the model (a kind of harness) limit unpredictability so that the model behaves consistently enough for its tasks. As developers our goal shouldn’t be to get rid of all non-determinism but manage it in a structured way with clear tools, specific constraints, automated checks, guardrails and quality gates. Techniques like LLM-as-a-judge also play an important role in our toolbox.
Trust isn’t absolute either; it depends on how critical the task is and how much confidence it demands. Sending an email vs generating an HTTP request vs processing a banking transaction are very different risks. Each task has its own level of verification, authorization and oversight.
- Sending an emailrisk level 1 of 3
- Generating an HTTP requestrisk level 2 of 3
- Executing a banking transactionrisk level 3 of 3
In this post I’ll share one way to keep coding agents on track so they don’t wander off when doing their intended tasks for long periods of time.
Reliability is engineered not inherited
Before we get into that let’s build a useful mental model for trust, reliability and determinism. Engineers every day build reliable products on systems that are fundamentally unreliable. The internet as an example: without the layers of engineering abstraction (OSI model) and protocols (IP/TCP) built on top of them we wouldn’t be able to develop applications that millions rely on today.
The underlying physical network is unreliable packets can get lost, delayed, duplicated or arrive out of order. We didn’t just assume that these things wouldn’t happen and built mechanisms to deal with them instead. TCP provides reliable ordered byte streams via sequence numbers, acknowledgements, retransmissions and flow control measures.
We didn’t build a perfectly reliable network, we built layers of abstraction and safeguards so that communication can happen reliably even though it’s not perfect. The same thing applies to AI agents instead of expecting an LLM to behave predictably all the time, we can design systems around it so that it knows what to do (but only outputs what it knows), validates its output and doesn’t let failures spread.
The internet
- UnreliablePhysical networkPackets are lost, delayed, duplicated or arrive out of order.
- EngineeredTCPSequence numbers, acknowledgments, retransmissions, flow and congestion control.
- What you build onReliable, ordered byte streamApplications that millions of people depend on.
A coding agent
- UnreliableThe LLMNon-deterministic output.
- EngineeredThe harnessConstrain behavior, validate outputs, enforce invariants, stop failures from propagating.
- What you build onReliable enough for the taskChanges you can trust at the level the task requires.
Check early, check mechanically
Since I started working with coding agents I’ve been trying to move validation as far left as possible and add mechanical checks in pre-commit hooks. Many projects I work on have a lefthook.yml file for Lefthook with commands for pre-commit and pre-push operations that trigger recipes in a justfile. Just is a command runner where I configure my entire build setup (tests, linters what works for the project). Whenever the agent needs to know what to run next it refers to that one file.
- The agentWhile editing, it reads the justfile to know what to run.
lefthook.ymlOn every commit and push, it calls the same recipes.
justfileOne place that defines every quality command.- Tests
- Linters
- Build
- Architecture checks
Here’s how it looks in a small TypeScript service: the justfile describes three recipes where each runs previous ones but adds more checks at each stage:
sample-service/AGENTS.mdHow to work here, and which recipe to run.justfileThe one place that defines every quality command.lefthook.ymlGit hooks that only call just recipes..github/workflows/ci.ymlRuns just ci as a required check on main.
knip.jsonUnused exports, dependencies and dead code..dependency-cruiser.cjsForbidden imports and layer boundaries.fta.jsonComplexity and maintainability limit for each file..oxlintrc.jsonFast first lint pass.eslint.config.jsSlower, type-aware lint rules.vitest.config.tsUnit and integration tests, and a coverage floor.stryker.config.jsonMutation testing with the TypeScript checker..gitleaks.tomlSecrets in staged changes.src/orders/One folder for each domain.domain/Pure rules. Imports nothing from app or infra.app/Use cases. Depend on domain only.infra/Database and HTTP adapters.
payments/
tests/e2e/End-to-end tests for pre-push and CI.
| Recipe | What it runs | Who calls it |
|---|---|---|
just quick | Formatting, Oxlint, type checking, tests for changed files and Gitleaks on staged changes | The agent while it edits, and the pre-commit hook |
just check |
| The pre-push hook |
just ci |
| CI, as a required check |
The agent executes same recipe called in the pre-commit hook so that successful edits result in passing commits.
Each file has a different job to do but only one guarantees success.
| Mechanism | Responsibility | Guarantee |
|---|---|---|
AGENTS.md | Explain how to work | Guidance |
justfile | Define the quality commands | Standardized execution |
| Enforce checks at workflow boundaries | Changes cannot progress through enforced paths when checks fail |
One tool for every kind of failure
These are some commands I usually have defined in my justfile, each one is targeting a different type of problem:
| Tool | What it catches |
|---|---|
| Knip | Unused exports, unused dependencies and dead code. |
| Dependency Cruiser | Broken dependency boundaries, forbidden imports and violated architectural constraints. |
| FTA (Fast TypeScript Analyzer) | Files whose complexity and maintainability scores pass a set limit. |
| Static code-quality rules and selected correctness checks. | |
anti-slop (Oxlint plugin) | Shortcuts that agents often take, such as unchecked type assertions, loose unknown types and module mocks in tests. |
| Regressions in behavior, through unit tests and end-to-end tests. | |
Stryker Mutator with the TypeScript checker | Tests that do not detect representative behavioral defects. |
| Gitleaks | Secrets, such as API keys and tokens, committed to the repository. |
No single tool guarantees correctness, each one targets a different vulnerability.
Run each check where it pays off
A good refinement is that you don’t need to run every check on every commit optimize your feedback loop by running each check at its most effective point.
- 01During editingFormatting, linting, type checking and focused tests.
- 02Pre-commitFast, relevant checks that catch common regressions.
- 03Pre-pushBroader tests and appropriate security or architecture checks.
- 04CIAuthoritative enforcement, including expensive checks such as mutation testing where appropriate.
At every stage, a failing check sends its output back to the agent, which repairs the change and runs the check again.
Let your agent explore but allow workflow decisions
Non-determinism shouldn’t make you distrust the agent, it’s something to prepare for (TCP does just that when dealing with unreliable network situations).
Your agent should always have some freedom in how it solves problems. Keep things simple: bundle your quality commands into one justfile connect fast ones into a pre-commit hook run resource intensive ones in CI. Let your agent be non deterministic but keep your workflow predictable.
