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

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.6
File Size Uploaded
autourgos_preiteration-3.0.6.tar.gz 25.0 kB Details

Built distribution (wheel)

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

Total release size: 44.3 kB

Release files / autourgos_preiteration-3.0.6.tar.gz

Download URL autourgos_preiteration-3.0.6.tar.gz
Size 25.0 kB
Tags Source
SHA-256 checksum
How to use checksums
ad214639ae697660d5f1cab4200911d763889fbd289953c6df940fc67df19bed
BLAKE2b-256 checksum
How to use checksums
531b4f0cacbb08c874fca401123a0931a4d58008d905df26b8cbb27bc5c468fe
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.6-py3-none-any.whl

Download URL autourgos_preiteration-3.0.6-py3-none-any.whl
Size 19.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6893055acd800543fae60f93ad8b42ee4317e58e23b7a1b555f7d34ab5f9f591
BLAKE2b-256 checksum
How to use checksums
2bfe01e2df219dc4bbc8544b2e7b6f618467c486478a0139a99cf86d7ae28c45
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

This release

3.0.6 This release

2 release files

3.0.2

2 release files

3.0.1

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