Skip to main content

autourgos-preiteration

Pre-iteration middleware for Autourgos agents.

Run any sync or async callback — and inject files like screenshots — before every agent iteration. Built-in image compression keeps vision LLM costs low.


Why use this?

Some agent tasks need fresh context every iteration:

  • Computer-use agents — take a screenshot before each step so the LLM sees the current screen state
  • Live data agents — refresh a price feed, sensor reading, or API response before each reasoning step
  • Monitoring agents — ping a health endpoint or log iteration metrics before the LLM call

PreIterationMiddleware handles all of this cleanly, with zero boilerplate inside your agent.


Install

pip install autourgos-preiteration

For image resize/compression support (Pillow):

pip install 'autourgos-preiteration[images]'

Zero required dependencies. Works with any Autourgos agent.


Quick Start

from autourgos_preiteration import PreIterationMiddleware
from autourgos_react_agent import ReactAgent

SCREENSHOT = "/tmp/screen.png"

def capture(iteration: int) -> None:
    take_screenshot(SCREENSHOT)  # your screenshot function

middleware = PreIterationMiddleware(
    callback=capture,
    files=SCREENSHOT,
    image_quality="low",  # ~85 tokens flat — great for computer-use agents
)

agent = ReactAgent(llm=my_llm, middleware=[middleware])
result = agent.invoke("Open the browser and search for Python 3.13 release notes")
print(result)

When used with a verbose ReactAgent (verbose=True), PreIterationMiddleware also narrates its own actions into the agent's trace, right alongside the Thought/Action/Observation lines, e.g.:

[PreIteration] Injected 1 file(s) before iteration 2.

Run multiple callbacks

SEQUENTIAL — one after another

from autourgos_preiteration import PreIterationMiddleware, SEQUENTIAL

def capture_screen(iteration: int) -> None:
    take_screenshot("/tmp/screen.png")

def log_step(iteration: int) -> None:
    print(f"Starting iteration {iteration}")

middleware = PreIterationMiddleware(
    callback=SEQUENTIAL[capture_screen, log_step]
)

PARALLEL — all at the same time

from autourgos_preiteration import PreIterationMiddleware, PARALLEL

def capture_screen(iteration: int) -> None:
    take_screenshot("/tmp/screen.png")

def refresh_cache(iteration: int) -> None:
    cache.clear()

def ping_health(iteration: int) -> None:
    requests.get("https://api.example.com/health")

middleware = PreIterationMiddleware(
    callback=PARALLEL[capture_screen, refresh_cache, ping_health]
)

Async callbacks

import asyncio
from autourgos_preiteration import PreIterationMiddleware

async def async_capture(iteration: int) -> None:
    await asyncio.sleep(0)  # non-blocking
    take_screenshot("/tmp/screen.png")

middleware = PreIterationMiddleware(callback=async_capture)

Works inside both agent.invoke() (sync) and agent.ainvoke() (async).


Dynamic file injection

Pass a callable for files to generate paths per iteration:

def get_screenshot_path(iteration: int) -> str:
    path = f"/tmp/screen_{iteration}.png"
    take_screenshot(path)
    return path

middleware = PreIterationMiddleware(files=get_screenshot_path, image_quality="medium")

Image quality

Control how much each screenshot costs in LLM tokens:

Value Max size JPEG quality OpenAI detail Approx tokens
"auto" (default) No resize No change "auto" Varies
"high" No resize No change "high" ~1000+
"medium" 768 px 70 "auto" ~300–500
"low" 512 px 60 "low" ~85 (flat)
int 1–100 512 px if ≤512 That value "low" / "auto" Varies

Resize requires Pillow: pip install 'autourgos-preiteration[images]'

If Pillow is not installed, the image_detail hint is still applied (token savings from the detail flag alone), but no resize/recompress happens.


Parameters

Parameter Type Default Description
callback callable or None None Sync/async function (iteration: int). Wrap with SEQUENTIAL or PARALLEL for multiple hooks.
files str, list, callable, or None None File path(s) to inject into LLM. Callable receives iteration number.
image_quality str or int "auto" Image compression level. See table above.

Combine with other middleware

from autourgos_preiteration import PreIterationMiddleware
from autourgos_history import AgentHistoryMiddleware
from autourgos_summarizer import AutoSummarizeMiddleware

middleware = [
    PreIterationMiddleware(callback=capture, files="/tmp/screen.png", image_quality="low"),
    AutoSummarizeMiddleware(summarize_every=5),
    AgentHistoryMiddleware(),
]
agent = ReactAgent(llm=my_llm, middleware=middleware)

Requirements

  • Python 3.9+
  • Pillow (optional) — for image resize: pip install 'autourgos-preiteration[images]'

Links


License

MIT — see LICENSE

Download files

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

Source Distribution

autourgos_preiteration-3.0.0.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

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

autourgos_preiteration-3.0.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file autourgos_preiteration-3.0.0.tar.gz.

File metadata

  • Download URL: autourgos_preiteration-3.0.0.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for autourgos_preiteration-3.0.0.tar.gz
Algorithm Hash digest
SHA256 13e730d49552eaf50d5fcc4e63c1d3cb093bdfa65ceff0539d6c8b18abc7dd57
MD5 68c3a58a8ad15069c120931cc5bc65e2
BLAKE2b-256 111d00497fa545b644be444e427a5443b6db1fc7cd8ce67cc4ad958842ea9a15

See more details on using hashes here.

File details

Details for the file autourgos_preiteration-3.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for autourgos_preiteration-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 81bd340344497f5bd018c272e84a5990c2eca033af4017973a4b58f4c7943dee
MD5 76ce5f75a1b98e6466d5028a26f49328
BLAKE2b-256 1030c27fa022f791800b5a6d8d39a61dbce135182e79c771904668fa11423c52

See more details on using hashes here.

Supported by

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