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.
from autourgos_preiteration import PreIterationMiddleware
from autourgos_agent import Agent
middleware = PreIterationMiddleware(callback=capture, files="/tmp/screen.png", image_quality="low")
agent = Agent(llm=my_llm, middleware=[middleware])
result = agent.invoke("Open the browser and search for Python 3.13 release notes")
Features
- Fresh context every iteration — screenshots, live data, health pings, whatever your callback produces
SEQUENTIAL/PARALLELcombinators for running multiple callbacks- Sync and async callbacks, both work inside
invoke()andainvoke() - Dynamic file injection — pass a callable for
filesto generate a fresh path per iteration - Built-in image compression (via optional Pillow) — from a flat ~85 tokens (
"low") to no resize ("auto") - Zero required dependencies; works with any Autourgos agent
Table of Contents
- Why Use This?
- Install
- Quick Start
- Run Multiple Callbacks
- Async Callbacks
- Dynamic File Injection
- Image Quality
- Parameters
- License
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]'
Quick Start
from autourgos_preiteration import PreIterationMiddleware
from autourgos_agent import Agent
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 = Agent(llm=my_llm, middleware=[middleware])
result = agent.invoke("Open the browser and search for Python 3.13 release notes")
print(result)
With Agent(verbose=True), this middleware also narrates its own actions into the agent's trace:
[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 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
| 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]'. Without Pillow, the image_detail
hint is still applied, 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. |
License
Apache License 2.0, Copyright (c) 2026 Jitin Kumar Sengar
Metadata
Release files for autourgos-preiteration 3.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| autourgos_preiteration-3.0.2.tar.gz | 23.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| autourgos_preiteration-3.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 41.5 kB
Release files / autourgos_preiteration-3.0.2.tar.gz
| Download URL | autourgos_preiteration-3.0.2.tar.gz |
|---|---|
| Size | 23.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4269a65339fae066eea65e65c1b61613f11d9f5a1527c47b72eb71de18bd9ba2
|
|
BLAKE2b-256 checksum How to use checksums |
526f91c7b64550deba0d03a33b0b69ef73db21cd17969daa3544d96a24176eb6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.9
|
Release files / autourgos_preiteration-3.0.2-py3-none-any.whl
| Download URL | autourgos_preiteration-3.0.2-py3-none-any.whl |
|---|---|
| Size | 18.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6c2a994d8cf8d029905077b617b39ea89ad67e8e2cf0452d959b867fb6563885
|
|
BLAKE2b-256 checksum How to use checksums |
6785a1e3263241f67ac8048550cdb986fe56a62778dd28690408048d1ae4a767
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.9
|