Skip to main content

autourgos-preiteration

Framework: Autourgos Python License: Apache 2.0 Author Contributor Contributor

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/PARALLEL combinators for running multiple callbacks
  • Sync and async callbacks, both work inside invoke() and ainvoke()
  • Dynamic file injection — pass a callable for files to 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?

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.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for autourgos-preiteration 3.0.1
File Size Uploaded
autourgos_preiteration-3.0.1.tar.gz 22.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for autourgos-preiteration 3.0.1
File Interpreter ABI Platform
autourgos_preiteration-3.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 40.6 kB

Release files / autourgos_preiteration-3.0.1.tar.gz

Download URL autourgos_preiteration-3.0.1.tar.gz
Size 22.5 kB
Tags Source
SHA-256 checksum
How to use checksums
13d500e829677b1aa7a87da0c12c810baaf38d44da4d68e06d5fa71a2f77ddba
BLAKE2b-256 checksum
How to use checksums
27c2050c74419d49ab48650dbf5ac81cc37dedc79a187687698b2bf4c81bc987
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.1-py3-none-any.whl

Download URL autourgos_preiteration-3.0.1-py3-none-any.whl
Size 18.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5498bfa458941e8092339c7d3ec5cd1f88db6e8efb5e8bcfa28245f7c1cc3cf9
BLAKE2b-256 checksum
How to use checksums
de2516aec7af18b0d0eed6642722137f692b71c1bc39e066e80e52abc25d836d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Release history Release notifications | RSS feed

3.1.0

2 release files

3.0.8

2 release files

3.0.7

2 release files

3.0.6

2 release files

3.0.2

2 release files

This release

3.0.1 This release

2 release files

3.0.0

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page