상위: Agno
요약
Agno는 3가지 Hook 체계로 도구 실행과 에이전트 응답을 가로채고 제어한다.
| Hook 유형 | 적용 범위 | 시그니처 | 설명 |
|---|---|---|---|
| pre_hook / post_hook | 개별 도구 | (fc: FunctionCall) | @tool 데코레이터에서 특정 도구 전/후 실행 |
| tool_hooks | Agent/Team 전체 | (name, call, args) | 모든 도구 호출을 래핑하는 미들웨어 |
| post_hooks (Agent) | 에이전트 응답 후 | — | 출력 검증, OutputCheckError 발생 |
pre_hook / post_hook (개별 도구)
FunctionCall 객체로 도구의 인자와 결과에 접근한다:
from agno.tools import FunctionCall, tool
def pre_hook(fc: FunctionCall):
print(f"About to call: {fc.function.name}, args: {fc.arguments}")
def post_hook(fc: FunctionCall):
print(f"Result: {fc.result}")
@tool(pre_hook=pre_hook, post_hook=post_hook)
def get_stories(agent: Agent) -> str:
...
비동기 훅도 지원: async def pre_hook(fc: FunctionCall): ...
tool_hooks (Agent/Team 수준 래퍼)
모든 도구 호출을 가로채는 미들웨어 패턴. 실행 시간 측정, 로깅, 인터셉트에 유용:
import time
from typing import Any, Callable, Dict
def logger_hook(function_name: str, function_call: Callable, arguments: Dict[str, Any]):
start = time.time()
result = function_call(**arguments) # 실제 도구 실행
print(f"{function_name} took {time.time() - start:.2f}s")
return result
agent = Agent(
tools=[HackerNewsTools()],
tool_hooks=[logger_hook], # 모든 도구에 적용
)
# Team에도 동일하게 적용 가능
team = Team(members=[agent], tool_hooks=[logger_hook])
RetryAgentRun — 조건부 재실행
post_hook에서 RetryAgentRun 예외를 발생시키면 에이전트가 자동으로 재실행된다. 도구 결과가 조건을 만족하지 않을 때 유용:
from agno.exceptions import RetryAgentRun
from agno.run import RunContext
from agno.tools import FunctionCall, tool
def validate_result(run_context: RunContext, fc: FunctionCall):
items = run_context.session_state.get("shopping_list", [])
if len(items) < 3:
raise RetryAgentRun(
f"Only {len(items)} items. Need at least 3. Add more."
)
@tool(post_hook=validate_result)
def add_item(run_context: RunContext, item: str) -> str:
"""Add an item to the shopping list."""
run_context.session_state["shopping_list"].append(item)
return f"Added {item}"
OutputCheckError — 출력 검증
Agent의 post_hooks에서 응답 품질을 검증하고 실패 시 OutputCheckError를 발생시킨다:
from agno.exceptions import OutputCheckError
# Agent 수준 post_hooks로 출력 검증
agent = Agent(
post_hooks=[simple_length_validation], # 응답 길이 등 검증
)
관련 노트
- Agno Tools & Guardrails — 도구·가드레일 전반
- Agno Agent