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

# Policy Feedback Loop

> LLM-driven feedback for policy violations with retry capabilities

## Overview

The **Policy Feedback Loop** enables LLM-generated feedback when policy violations occur, allowing agents to self-correct their outputs through retry loops.

## User Policy Feedback

Give users constructive guidance instead of hard blocking:

```python theme={null}
from upsonic import Agent, Task
from upsonic.safety_engine.policies.crypto_policies import CryptoBlockPolicy

agent = Agent(
    "anthropic/claude-sonnet-4-6",
    user_policy=CryptoBlockPolicy,
    user_policy_feedback=True,
    debug=True
)

task = Task(
    description="How can I buy Bitcoin and invest in cryptocurrency?"
)

result = agent.print_do(task)
print(result)
```

## Agent Policy Feedback

Enable agents to self-correct when their output violates policies. The agent retries until its output is compliant:

```python theme={null}
from upsonic import Agent, Task
from upsonic.safety_engine.policies.crypto_policies import CryptoBlockPolicy

agent = Agent(
    "anthropic/claude-sonnet-4-6",
    agent_policy=CryptoBlockPolicy,
    agent_policy_feedback=True,
    agent_policy_feedback_loop=3,
    debug=True
)

task = Task(
    description="Write a comprehensive guide about all investment types: stocks, bonds, real estate, cryptocurrency, and commodities"
)

result = agent.print_do(task)
print(result)
```

## How it Works

1. Agent generates a response (e.g., a guide including cryptocurrency section)
2. Agent policy detects a violation in the output
3. Feedback is sent back to the agent explaining the violation
4. Agent retries, generating a compliant response (e.g., guide without cryptocurrency)
5. Agent policy passes — compliant output is returned

The `agent_policy_feedback_loop` parameter controls how many retry attempts are allowed before the agent gives up.

## Async Support

Policy feedback loops work with async execution:

```python theme={null}
import asyncio
from upsonic import Agent, Task
from upsonic.safety_engine.policies.crypto_policies import CryptoBlockPolicy

async def main():
    agent = Agent(
        "anthropic/claude-sonnet-4-6",
        agent_policy=CryptoBlockPolicy,
        agent_policy_feedback=True,
        agent_policy_feedback_loop=3,
        debug=True,
    )

    task = Task(
        description="Write a guide about investment types including crypto"
    )

    result = await agent.print_do_async(task)
    print(result)

asyncio.run(main())
```
