Skip to main content

A Dual-State Agent Framework for reliable LLM code generation with guard-validated loops

Project description

AtomicGuard

CI codecov PyPI version Python versions License: MIT

A Dual-State Agent Framework for reliable LLM code generation.

Why AtomicGuard?

AI agents hallucinate. Worse, those hallucinations compound — each generation builds on the last, and errors propagate through the workflow.

AtomicGuard solves this by combining to aspects - decompose goals into small measurable tasks and through Bounded Indeterminacy: the LLM generates content, but a deterministic state machine controls the logic. Every generation is validated before the workflow advances.

Challenge Solution
🛡️ Safety Dual-State Architecture & Atomic Action Pairs
💾 State Versioned Repository Items & Configuration Snapshots
🌐 Scale Multi-Agent Coordination via Shared DAG
📈 Improvement Continuous Learning from Guard Verdicts

Learn more about the architecture

New to AtomicGuard? Start with the Getting Started Guide.

Paper: Managing the Stochastic: Foundations of Learning in Neuro-Symbolic Systems for Software Engineering (Thompson, 2025)

Overview

AtomicGuard implements guard-validated generation loops that dramatically improve LLM reliability. The core abstraction is the Atomic Action Pair ⟨agen, G⟩ — coupling each generation action with a validation guard.

Key results (Yi-Coder 9B, n=50):

Task Baseline Guarded Improvement
Template 35% 90% +55pp
Password 82% 98% +16pp
LRU Cache 94% 100% +6pp

Installation

# From PyPI
pip install atomicguard

# From source
git clone https://github.com/thompsonson/atomicguard.git
cd atomicguard
uv venv && source .venv/bin/activate
uv pip install -e ".[dev,test]"

Quick Start

from atomicguard import (
    OllamaGenerator, SyntaxGuard, TestGuard,
    CompositeGuard, ActionPair, DualStateAgent,
    InMemoryArtifactDAG
)

# Setup
generator = OllamaGenerator(model="qwen2.5-coder:7b")
guard = CompositeGuard([SyntaxGuard(), TestGuard("assert add(2, 3) == 5")])
action_pair = ActionPair(generator=generator, guard=guard)
agent = DualStateAgent(action_pair, InMemoryArtifactDAG(), rmax=3)

# Execute
artifact = agent.execute("Write a function that adds two numbers")
print(artifact.content)

See examples/ for more detailed usage, including a mock example that works without an LLM.

LLM Backends

AtomicGuard supports multiple LLM backends. Each generator implements GeneratorInterface and can be swapped in with no other code changes.

Ollama (local or cloud)

Uses the OpenAI-compatible API. Works with any Ollama-served model:

from atomicguard.infrastructure.llm import OllamaGenerator

# Local instance (default: http://localhost:11434/v1)
generator = OllamaGenerator(model="qwen2.5-coder:7b")

HuggingFace Inference API

Connects to HuggingFace Inference Providers via huggingface_hub. Supports any model available through the HF Inference API, including third-party providers like Together AI.

# Install the optional dependency
pip install huggingface_hub

# Set your API token
export HF_TOKEN="hf_your_token_here"
from atomicguard.infrastructure.llm import HuggingFaceGenerator
from atomicguard.infrastructure.llm.huggingface import HuggingFaceGeneratorConfig

# Default: Qwen/Qwen2.5-Coder-32B-Instruct
generator = HuggingFaceGenerator()

# Custom model and provider
generator = HuggingFaceGenerator(HuggingFaceGeneratorConfig(
    model="Qwen/Qwen2.5-Coder-32B-Instruct",
    provider="together",       # or "auto", "hf-inference"
    temperature=0.7,
    max_tokens=4096,
))

Drop-in replacement in any workflow:

from atomicguard import (
    SyntaxGuard, TestGuard, CompositeGuard,
    ActionPair, DualStateAgent, InMemoryArtifactDAG
)
from atomicguard.infrastructure.llm import HuggingFaceGenerator

generator = HuggingFaceGenerator()
guard = CompositeGuard([SyntaxGuard(), TestGuard("assert add(2, 3) == 5")])
action_pair = ActionPair(generator=generator, guard=guard)
agent = DualStateAgent(action_pair, InMemoryArtifactDAG(), rmax=3)

artifact = agent.execute("Write a function that adds two numbers")
print(artifact.content)

Benchmarks

Run the simulation from the paper:

python -m benchmarks.simulation --model yi-coder:9b --trials 50 --task all --output results/results.db --format sqlite

# Generate report
python -m benchmarks.simulation --visualize --output results/results.db --format sqlite

Project Structure

atomicguard/
├── src/atomicguard/     # Core library
├── benchmarks/          # Simulation code
├── docs/design/         # Design documents
├── examples/            # Usage examples
└── results/             # Generated reports & charts

Citation

If you use this framework in your research, please cite the paper:

Thompson, M. (2025). Managing the Stochastic: Foundations of Learning in Neuro-Symbolic Systems for Software Engineering. arXiv preprint arXiv:2512.20660.

@misc{thompson2025managing,
  title={Managing the Stochastic: Foundations of Learning in Neuro-Symbolic Systems for Software Engineering},
  author={Thompson, Matthew},
  year={2025},
  eprint={2512.20660},
  archivePrefix={arXiv},
  primaryClass={cs.LG},
  url={https://arxiv.org/abs/2512.20660}
}

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

atomicguard-2.5.0.tar.gz (97.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

atomicguard-2.5.0-py3-none-any.whl (116.8 kB view details)

Uploaded Python 3

File details

Details for the file atomicguard-2.5.0.tar.gz.

File metadata

  • Download URL: atomicguard-2.5.0.tar.gz
  • Upload date:
  • Size: 97.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for atomicguard-2.5.0.tar.gz
Algorithm Hash digest
SHA256 c7f535214a361e81b91fcadacd957701a15894c2481ec6e155e7bdbdbfca6bda
MD5 7df330af653ef0e22b9fae1cf58f5718
BLAKE2b-256 591a3cf363e496aaf8ebbfc525bd182afe27f9ce73df0543cc4c453a420c7d07

See more details on using hashes here.

Provenance

The following attestation bundles were made for atomicguard-2.5.0.tar.gz:

Publisher: release.yml on thompsonson/atomicguard

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file atomicguard-2.5.0-py3-none-any.whl.

File metadata

  • Download URL: atomicguard-2.5.0-py3-none-any.whl
  • Upload date:
  • Size: 116.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for atomicguard-2.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 416207a19741a77565242ce6a43a982f46af0cccec8258fc6cee75746cd794dd
MD5 434d540265066fbe5550e4ffb572ce30
BLAKE2b-256 a752863608c5a39124aa50a7bdd5b0b56542a6bfbccabf193774095a2a33cee3

See more details on using hashes here.

Provenance

The following attestation bundles were made for atomicguard-2.5.0-py3-none-any.whl:

Publisher: release.yml on thompsonson/atomicguard

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page