> ## Documentation Index
> Fetch the complete documentation index at: https://ikun.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 重试函数

本示例展示了如何在函数调用失败或您不满意其输出时重试该函数调用。这对于以下场景非常有用：

* 处理临时故障
* 通过重试提高输出质量
* 实现人工干预验证

## 代码

```python retry_functions.py theme={null}
from typing import Iterator

from agno.agent import Agent
from agno.exceptions import RetryAgentRun
from agno.tools import FunctionCall, tool

num_calls = 0


def pre_hook(fc: FunctionCall):
    global num_calls

    print(f"Pre-hook: {fc.function.name}")
    print(f"Arguments: {fc.arguments}")
    num_calls += 1
    if num_calls < 2:
        raise RetryAgentRun(
            "This wasn't interesting enough, please retry with a different argument"
        )


@tool(pre_hook=pre_hook)
def print_something(something: str) -> Iterator[str]:
    print(something)
    yield f"I have printed {something}"


agent = Agent(tools=[print_something], markdown=True)
agent.print_response("Print something interesting", stream=True)
```

## 使用方法

<Steps>
  <Snippet file="create-venv-step.mdx" />

  <Step title="安装库文件">
    ```bash theme={null}
    pip install openai agno
    ```
  </Step>

  <Step title="运行代理">
    ```bash theme={null}
    python retry_functions.py
    ```
  </Step>
</Steps>
