---
title: "Keeping your agents in lane · Sérgio Serra"
description: "LLMs are non-deterministic. The system you build around them does not have to be."
url: "https://sergioserra.me/articles/keeping-your-agents-in-lane/"
---

October 13, 2026 · 5 min read

# Keeping your agents in lane

LLMs are non-deterministic. The system you build around them does not have to be.

TL;DR

- LLMs are non-deterministic, but the system around them can be deterministic.
- Trust is not binary. The level of verification depends on how critical the task is.
- Put every quality command in one justfile, so that the agent and the hooks run the same checks.
- Use one tool for each kind of failure, and run each check at the earliest point where it is worth its cost.
- Let the agent choose how it works. Let the workflow decide what gets merged.

![Black-and-white overhead view of a multi-lane elevated highway, with dashed lane markings and chevron-striped barriers along both edges.](https://sergioserra.me/_astro/keeping-your-agents-in-lane.D47EoN7Q_ZPePXA.webp)

On this page

1. [Reliability is engineered not inherited](https://sergioserra.me/articles/keeping-your-agents-in-lane/#reliability-is-engineered-not-inherited)
2. [Check early, check mechanically](https://sergioserra.me/articles/keeping-your-agents-in-lane/#check-early-check-mechanically)
3. [One tool for every kind of failure](https://sergioserra.me/articles/keeping-your-agents-in-lane/#one-tool-for-every-kind-of-failure)
4. [Run each check where it pays off](https://sergioserra.me/articles/keeping-your-agents-in-lane/#run-each-check-where-it-pays-off)
5. [Let your agent explore but allow workflow decisions](https://sergioserra.me/articles/keeping-your-agents-in-lane/#let-your-agent-explore-but-allow-workflow-decisions)

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.

1. Sending an emailrisk level 1 of 3
2. Generating an HTTP requestrisk level 2 of 3
3. Executing a banking transactionrisk level 3 of 3

Lower riskHigher risk

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

1. Unreliable**Physical network**Packets are lost, delayed, duplicated or arrive out of order.
2. Engineered**TCP**Sequence numbers, acknowledgments, retransmissions, flow and congestion control.
3. What you build on**Reliable, ordered byte stream**Applications that millions of people depend on.

A coding agent

1. Unreliable**The LLM**Non-deterministic output.
2. Engineered**The harness**Constrain behavior, validate outputs, enforce invariants, stop failures from propagating.
3. What you build on**Reliable enough for the task**Changes 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](https://github.com/evilmartians/lefthook) with commands for pre-commit and pre-push operations that trigger recipes in a `justfile`. [Just](https://github.com/casey/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 agent**While editing, it reads the justfile to know what to run.
- `lefthook.yml`On every commit and push, it calls the same recipes.

`justfile`One place that defines every quality command.

- Tests
- Linters
- Build
- Architecture checks

The agent and the Git hooks call the same recipes. What the agent runs while it edits is what blocks the commit.

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/`
  - \# workflow
  - `AGENTS.md`How to work here, and which recipe to run.
  - `justfile`The one place that defines every quality command.
  - `lefthook.yml`Git hooks that only call just recipes.
  - `.github/`
    - `workflows/`
      - `ci.yml`Runs just ci as a required check on main.
  - \# quality tools
  - `knip.json`Unused exports, dependencies and dead code.
  - `.dependency-cruiser.cjs`Forbidden imports and layer boundaries.
  - `fta.json`Complexity and maintainability limit for each file.
  - `.oxlintrc.json`Fast first lint pass.
  - `eslint.config.js`Slower, type-aware lint rules.
  - `vitest.config.ts`Unit and integration tests, and a coverage floor.
  - `stryker.config.json`Mutation testing with the TypeScript checker.
  - `.gitleaks.toml`Secrets in staged changes.
  - \# code
  - `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.

A sample repository. Each file at the root has one job, and every check that the hooks and CI run is defined once, in the justfile.

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`

`quick`, plus ESLint, Dependency Cruiser, Knip, FTA and the full test suite

The pre-push hook

`just ci`

`check`, plus end-to-end tests and mutation testing

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

`lefthook.yml` + CI

Enforce checks at workflow boundaries

Changes cannot progress through enforced paths when checks fail

Instructions guide the agent. Only the hooks and CI stop a change.

### 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](https://knip.dev/)

Unused exports, unused dependencies and dead code.

[Dependency Cruiser](https://github.com/sverweij/dependency-cruiser)

Broken dependency boundaries, forbidden imports and violated architectural constraints.

[FTA (Fast TypeScript Analyzer)](https://ftaproject.dev/)

Files whose complexity and maintainability scores pass a set limit.

[ESLint](https://eslint.org/), [Oxlint](https://oxc.rs/docs/guide/usage/linter.html)

Static code-quality rules and selected correctness checks.

[anti-slop](https://github.com/dmmulroy/anti-slop) (Oxlint plugin)

Shortcuts that agents often take, such as unchecked type assertions, loose unknown types and module mocks in tests.

[Vitest](https://vitest.dev/), [Bun](https://bun.sh/)

Regressions in behavior, through unit tests and end-to-end tests.

[Stryker Mutator](https://stryker-mutator.io/) with the [TypeScript checker](https://stryker-mutator.io/docs/stryker-js/typescript-checker/)

Tests that do not detect representative behavioral defects.

[Gitleaks](https://github.com/gitleaks/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.

Fast, runs oftenSlow, authoritative

1. 01**During editing**Formatting, linting, type checking and focused tests.
2. 02**Pre-commit**Fast, relevant checks that catch common regressions.
3. 03**Pre-push**Broader tests and appropriate security or architecture checks.
4. 04**CI**Authoritative 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.

Each stage runs the checks that are worth their cost at that point. Cheap checks run on every edit. Expensive checks wait for CI.

## 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.**

Written by

[![](https://sergioserra.me/_astro/portrait.Bc2fIQHo_HS1RV.webp)Sérgio Serra](https://sergioserra.me/)

Staff engineer turned AI engineer. I help businesses of all sizes adopt AI across their technology and their teams, from agentic workflows and cloud agents to AI-powered development practices that ship. I design systems with agentic workflows, MCP, AI skills and second-brain systems, and I build with Claude Code and Cursor every day.

[Book a free call→](https://calendly.com/sergio-f-serra/30min)[What I help with](https://sergioserra.me/services/)
