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")
  • Concurrency-safe — per-run resolved files/temp-file state isolated via contextvars.ContextVar (autourgos-core's RunScopedState), correct for both concurrent threads and concurrent asyncio tasks sharing one thread
  • 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.1.0

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.1.0
File Size Uploaded
autourgos_preiteration-3.1.0.tar.gz 26.4 kB Details

Built distribution (wheel)

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

Total release size: 46.1 kB

Release files / autourgos_preiteration-3.1.0.tar.gz

Download URL autourgos_preiteration-3.1.0.tar.gz
Size 26.4 kB
Tags Source
SHA-256 checksum
How to use checksums
f380d2a29a2def3800d1b9ee606d726172d65efcedbafc701824e28286101795
BLAKE2b-256 checksum
How to use checksums
3354f86120fdce7757564499e2ba8a16d49be19f98b9b5bb1454e6719a855e95
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.1.0-py3-none-any.whl

Download URL autourgos_preiteration-3.1.0-py3-none-any.whl
Size 19.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3e6a60373b9d7c13565595ab132b876e37ed9eeadd4ffb41c628991c192d6cc0
BLAKE2b-256 checksum
How to use checksums
be161a29190f836f5974f9e807ef25eae4e0671b8fb86675ce28104360bba0ff
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

This release

3.1.0 This release

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

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