Created
March 11, 2026 15:02
-
-
Save x/2fc535dcae115f63e11e29a2a66c1840 to your computer and use it in GitHub Desktop.
simple_writer_critic.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import asyncio | |
| import logging | |
| import os | |
| from agents import Agent, AgentHooks, Runner, function_tool | |
| from agents.extensions.models.litellm_model import LitellmModel | |
| from agents.items import ModelResponse | |
| MODEL = LitellmModel(model="vertex_ai/gemini-2.0-flash-lite", api_key="unused") | |
| # This hook is not necessary, it's just to help log what the agents are doing | |
| class LoggerHook(AgentHooks): | |
| async def on_llm_end(self, context, agent, response: ModelResponse): | |
| for item in response.output: | |
| if hasattr(item, "content"): | |
| text = "".join(c.text for c in item.content if hasattr(c, "text")) | |
| if text: | |
| print(f"[{agent.name}] said: {text}") | |
| if hasattr(item, "name"): | |
| print(f"[{agent.name}] called tool: {item.name}({item.arguments})") | |
| # --- Tools -------------------------------------------------------------------- | |
| @function_tool | |
| def check_length(summary: str) -> bool: | |
| """ | |
| Checks the length of the summary. | |
| Args: | |
| summary: The summary being checked. | |
| Returns: | |
| bool, if the length is too long. True if short enough, false if too long. | |
| """ | |
| return len(summary) < 200 | |
| @function_tool | |
| def check_is_capitalized(summary: str) -> bool: | |
| """ | |
| Checks that the summary starts with a capital letter. | |
| Args: | |
| summary: The summary being checked. | |
| Returns: | |
| True if the first letter is capitalized, False otherwise. | |
| """ | |
| return summary[0].isupper() | |
| # --- Agents ------------------------------------------------------------------- | |
| critic_agent = Agent( | |
| name="Checker Agent", | |
| instructions="You check the work of the writer_agent using your tools. Use all of them. Convey back what kinds of checks were failed.", | |
| model=MODEL, | |
| tools=[check_length, check_is_capitalized], | |
| hooks=LoggerHook(), | |
| ) | |
| writer_agent = Agent( | |
| name="Writer Agent", | |
| instructions="Your job is to write a short summary of a subject for the user. After writing, you MUST call the critic agent to check your summary. If the critic reports any failed checks, revise your summary to fix those issues and call the critic again. Repeat until all checks pass, then return the final summary.", | |
| model=MODEL, | |
| tools=[ | |
| critic_agent.as_tool( | |
| tool_name="call_critic_agent", | |
| tool_description="Critique the work of the writer", | |
| ), | |
| ], | |
| hooks=LoggerHook(), | |
| ) | |
| async def main(): | |
| output = await Runner.run(writer_agent, "Write me a summary of World War 1") | |
| print(output) | |
| if __name__ == "__main__": | |
| asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment