Modifier Checker feedback Loop

This example demonstrates a simple multi-agent example designed to show how two agents interact in a feedback loop.

  1. Modifier — modifies the message value (decrements by 1).

  2. Checker — checks if the value meets the stopping condition. If not, it sends it back.

This creates a loop until the check condition is satisfied.

import asyncio
from dataclasses import dataclass
from typing import Callable

from agentopera.engine import DefaultMessageChannel, MessageContext, RoutedAgent, default_subscription, message_handler
@dataclass
class Message:
    content: int
@default_subscription
class Modifier(RoutedAgent):
    def __init__(self, modify_val: Callable[[int], int]) -> None:
        super().__init__("A modifier agent.")
        self._modify_val = modify_val

    @message_handler
    async def handle_message(self, message: Message, ctx: MessageContext) -> None:
        val = self._modify_val(message.content)
        print(f"{'-'*80}\nModifier:\nModified {message.content} to {val}")
        await self.publish_message(Message(content=val), DefaultMessageChannel())  # type: ignore
@default_subscription
class Checker(RoutedAgent):
    def __init__(self, run_until: Callable[[int], bool]) -> None:
        super().__init__("A checker agent.")
        self._run_until = run_until

    @message_handler
    async def handle_message(self, message: Message, ctx: MessageContext) -> None:
        if not self._run_until(message.content):
            print(f"{'-'*80}\nChecker:\n{message.content} passed the check, continue.")
            await self.publish_message(Message(content=message.content), DefaultMessageChannel())
        else:
            print(f"{'-'*80}\nChecker:\n{message.content} failed the check, stopping.")
async def main() -> None:
    from agentopera.engine import AgentId, LocalAgentEngine

    engine = LocalAgentEngine()

    await Modifier.register(
        engine,
        "modifier",
        lambda: Modifier(modify_val=lambda x: x - 1),
    )

    await Checker.register(
        engine,
        "checker",
        lambda: Checker(run_until=lambda x: x <= 1),
    )

    engine.start()
    await engine.send_message(Message(10), AgentId("checker", "default"))
    await engine.stop_when_idle()

await main()
--------------------------------------------------------------------------------
Checker:
10 passed the check, continue.
--------------------------------------------------------------------------------
Modifier:
Modified 10 to 9
--------------------------------------------------------------------------------
Checker:
9 passed the check, continue.
--------------------------------------------------------------------------------
Modifier:
Modified 9 to 8
--------------------------------------------------------------------------------
Checker:
8 passed the check, continue.
--------------------------------------------------------------------------------
Modifier:
Modified 8 to 7
--------------------------------------------------------------------------------
Checker:
7 passed the check, continue.
--------------------------------------------------------------------------------
Modifier:
Modified 7 to 6
--------------------------------------------------------------------------------
Checker:
6 passed the check, continue.
--------------------------------------------------------------------------------
Modifier:
Modified 6 to 5
--------------------------------------------------------------------------------
Checker:
5 passed the check, continue.
--------------------------------------------------------------------------------
Modifier:
Modified 5 to 4
--------------------------------------------------------------------------------
Checker:
4 passed the check, continue.
--------------------------------------------------------------------------------
Modifier:
Modified 4 to 3
--------------------------------------------------------------------------------
Checker:
3 passed the check, continue.
--------------------------------------------------------------------------------
Modifier:
Modified 3 to 2
--------------------------------------------------------------------------------
Checker:
2 passed the check, continue.
--------------------------------------------------------------------------------
Modifier:
Modified 2 to 1
--------------------------------------------------------------------------------
Checker:
1 failed the check, stopping.

Done!

This loop demonstrates a simple distributed message feedback loop between two agents.

Last updated