Static analysis for LLM agent code AG

Find the loops that make your AI agent expensive.

AgentGuard reads your Python without running it, points to the exact line, and estimates what a run costs.

Get Started → ↓ View Example

See exactly what AgentGuard catches.

Real output of AgentGuard 0.3.1 on examples/research_agent.py, a demo agent in the repo with five planted problems.

agentguard 0.3.1 · examples/research_agent.py
$ agentguard examples/research_agent.py
    ___                    __  ______                     __
   /   | ____ ____  ____  / /_/ ____/_  ______ __________/ /
  / /| |/ __ `/ _ \/ __ \/ __/ / __/ / / / __ `/ ___/ __  /
 / ___ / /_/ /  __/ / / / /_/ /_/ / /_/ / /_/ / /  / /_/ /
/_/  |_\__, /\___/_/ /_/\__/\____/\__,_/\__,_/_/   \__,_/
      /____/
Find LLM cost risks in agent code, before it runs · v0.3.1
════════════════════════════════════════════════════════════════════════
 
FILE ANALYSIS
────────────────────────────────────────────────────────────────────────
 
examples/research_agent.py [🐍 py]
Call sites: 3 • Loops: 3 • Risk: ⚠ CRITICAL
 
Findings:
⚠ line 31 AG001 LLM call (anthropic.messages) inside `while` loop (line 28) with no iteration cap
→ Bound the loop with a step counter (e.g. `for step in range(MAX_STEPS)`) or an explicit `if step >= MAX_STEPS: break`.
⚠ line 31 AG002 LLM call (anthropic.messages) re-sends a history that grows every iteration; input tokens grow quadratically
→ Keep a sliding window (`messages[-k:]`), summarise old turns, or use server-side compaction/context editing where the provider offers it.
⚡ line 31 AG007 ~2,260-token static prompt re-sent each iteration with no `cache_control`
→ Mark the static prefix with `cache_control` - cached reads bill at about 10% of the input price.
⚡ line 47 AG003 call to `summarise` inside 2 nested loops (lines 45, 46); calls multiply
→ Flatten the work into one batch, or cap the inner loop.
• line 21 AG006 LLM call (openai.chat) sets no output-token cap
→ Pass `max_tokens` / `max_completion_tokens` / `max_output_tokens`.
 
Cost per run by loop depth (N = iterations of open-ended loops):
N= 1 $0.0082 → $0.02 (2 calls)
N= 10 $0.21 → $0.49 (110 calls)
N=100 $14.88 → $36.40 (10,100 calls) ⚠ over $10 per run
 
════════════════════════════════════════════════════════════════════════
 
COST SUMMARY
────────────────────────────────────────────────────────────────────────
Default model: claude-sonnet-5-5
Files scanned: 1
Files with LLM calls: 1
LLM call sites: 3
────────────────────────────────────────────────────────────────────────
Cost per run (N=1): $0.0082 → $0.02
Cost per run (N=10): $0.21 → $0.49
Cost per run (N=100): $14.88 → $36.40
 
Assumes 500 system + 500 input tokens and 200-1000 output tokens per call; prices as of 2026-10-11.
 
════════════════════════════════════════════════════════════════════════
 
TOP RISK FILES
────────────────────────────────────────────────────────────────────────
examples/research_agent.py (2 high, 2 medium, 1 low)
 
════════════════════════════════════════════════════════════════════════
 
RECOMMENDATIONS
────────────────────────────────────────────────────────────────────────
1. [AG001] Bound the loop with a step counter (e.g. `for step in range(MAX_STEPS)`) or an explicit `if step >= MAX_STEPS: break`.
2. [AG002] Keep a sliding window (`messages[-k:]`), summarise old turns, or use server-side compaction/context editing where the provider offers it.
3. [AG003] Flatten the work into one batch, or cap the inner loop.
4. [AG007] Mark the static prefix with `cache_control` - cached reads bill at about 10% of the input price.
5. [AG006] Pass `max_tokens` / `max_completion_tokens` / `max_output_tokens`.
 
════════════════════════════════════════════════════════════════════════
5 finding(s), 2 high, in 1 file(s) · agentguard --list-rules for details
$

Three steps. No API key.

01

Install AgentGuard

One pip command. No API key and no sign-up: AgentGuard never calls a model or runs your code.

pip install agentguard-lint
02

Navigate to your project

Point your terminal at any Python project directory containing agent code.

cd your_project
03

See the cost risks

Each finding comes with a file, a line, a severity and a fix, plus a cost range per run.

agentguard .

One command to get started.

No API key, no sign-up, no runtime dependencies. Python 3.9 or newer.

$ pip install agentguard-lint
If agentguard isn't found (pip's scripts folder is not on PATH), run:
$ python -m agentguard .

Run it. That's it.

Run it from your project folder. It scans every Python file in it and its subfolders, skipping virtual environments and hidden folders.

⭐ Recommended — if PATH is configured
$ agentguard .
Fallback — works everywhere without PATH
$ python -m agentguard .
// WHAT DOES "." MEAN?
"." = current folder. AgentGuard will recursively scan:
→ All .py files in the current folder
→ All subfolders inside it, automatically
💡
TIP — Run from your project root.
python -m agentguard . crawls subfolders automatically, so running it from the root directory gives you full coverage in one pass. Running it from a subfolder only scans that subfolder and below.
Method Works Without PATH Recommended
agentguard . ❌ Needs PATH ⭐ Best
python -m agentguard . ✅ Yes Fallback
It refuses to scan a home folder or a whole drive, which is never one project.

Seven rules, each with a fix.

Every finding names the file, the line and the rule. Rule reference →

🔁

Uncapped agent loops

AG001. An LLM call in a loop that stops only when the model says so. Step counters, if step >= MAX: break, countdowns and retry helpers count as caps.

📈

Quadratic history growth

AG002. A conversation history that grows every turn and is re-sent in full. Trimming with messages[-k:] or summarising is recognised.

🌀

Nested loops and recursion

AG003 and AG005. Calls multiplied by nested loops, and functions that call a model and themselves with no depth parameter.

💸

Cost per run

Each call priced at the model named in the code, at 1, 10 and 100 loop iterations. Also flags missing max_tokens (AG006), one call per item (AG004) and uncached Anthropic prompts (AG007).

92%
precision on 8 held-out open-source projects, every finding checked by hand
99%
how far too low the naive "N calls" estimate was after 40 measured turns
~12k
lines per second; 1.9M lines of agent code scanned for the benchmark

Agent costs grow with the square of the turns.

An LLM API keeps no memory, so an agent re-sends the whole conversation on every turn. Turn 40 pays for the 39 turns before it. Pricing an agent as "number of calls × cost of one call" misses this completely.

In a measured 40-turn run on a local model, that naive estimate was 99% too low. AgentGuard, calibrated on the first two turns, was 11% low.

Run on 22 open-source agent projects, it found the pattern in OpenAI's own swarm library, whose turn limit defaults to infinity. How it was measured →