A custom plugin that counts agent and LLM invocations.
| 26 | |
| 27 | |
| 28 | class CountInvocationPlugin(BasePlugin): |
| 29 | """A custom plugin that counts agent and LLM invocations.""" |
| 30 | |
| 31 | def __init__(self) -> None: |
| 32 | """Initialize the plugin with counters.""" |
| 33 | super().__init__(name="count_invocation") |
| 34 | self.agent_count: int = 0 |
| 35 | self.llm_request_count: int = 0 |
| 36 | |
| 37 | async def before_agent_callback( |
| 38 | self, *, agent: BaseAgent, callback_context: CallbackContext |
| 39 | ) -> None: |
| 40 | """Count agent runs.""" |
| 41 | self.agent_count += 1 |
| 42 | print(f"[Plugin] Agent run count: {self.agent_count}") |
| 43 | |
| 44 | async def before_model_callback( |
| 45 | self, *, callback_context: CallbackContext, llm_request: LlmRequest |
| 46 | ) -> None: |
| 47 | """Count LLM requests.""" |
| 48 | self.llm_request_count += 1 |
| 49 | print(f"[Plugin] LLM request count: {self.llm_request_count}") |
| 50 | |
| 51 | |
| 52 | root_agent = Agent( |