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

# Safe Regeneration

> Automatically regenerate AI content that scores below your quality threshold.

Safe Regeneration is a feedback loop: evaluate a response, and if it falls below your threshold, regenerate it with targeted improvement instructions and score it again, repeating until it passes. If the iteration limit is reached, the highest-scoring version produced is returned.

<Info>
  **API endpoint:** `POST /railscore/v1/safe-regenerate` | **Python:** `client.safe_regenerate()` | **JavaScript:** `client.safeRegenerate()`
</Info>

## How it works

```mermaid theme={null}
flowchart TD
    Start["Initial response"] --> Eval["Evaluate\n(RAIL Score)"]
    Eval --> Check{"Score >= threshold?"}
    Check -- yes --> Return["Return response"]
    Check -- no --> Regen["Regenerate with\nimprovement instructions"]
    Regen --> Eval
    Regen --> Limit{"Max iterations\nreached?"}
    Limit -- yes --> Best["Return best response\n(highest scored)"]
```

Each iteration costs the same credits as a standalone evaluation. Up to 5 iterations per request.

## Basic usage

<CodeGroup>
  ```python Python theme={null}
  from rail_score_sdk import RailScoreClient

  client = RailScoreClient(api_key="YOUR_RAIL_API_KEY")

  result = client.safe_regenerate(
      content="When reviewing resumes, prioritize Ivy League graduates.",
      prompt="What hiring criteria should I use?",
      threshold=7.0,
      mode="basic",
      max_iterations=3,
  )

  print(f"Final score: {result.final_score}/10")
  print(f"Iterations: {result.iterations_taken}")
  print(f"Content: {result.content}")
  ```

  ```typescript JavaScript theme={null}
  import { RailScoreClient } from "@responsible-ai-labs/rail-score";

  const client = new RailScoreClient({ apiKey: "YOUR_RAIL_API_KEY" });

  const result = await client.safeRegenerate({
    content: "When reviewing resumes, prioritize Ivy League graduates.",
    prompt: "What hiring criteria should I use?",
    threshold: 7.0,
    mode: "basic",
    maxIterations: 3,
  });

  console.log(`Final score: ${result.finalScore}/10`);
  console.log(`Content: ${result.content}`);
  ```

  ```bash cURL theme={null}
  curl -X POST https://api.responsibleailabs.ai/railscore/v1/safe-regenerate \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer YOUR_RAIL_API_KEY" \
    -d '{
      "content": "When reviewing resumes, prioritize Ivy League graduates.",
      "prompt": "What hiring criteria should I use?",
      "threshold": 7.0,
      "mode": "basic",
      "max_iterations": 3
    }'
  ```
</CodeGroup>

## The response

A result tells you both the outcome and what it took to get there:

* `content` — the final text to use (the passing version, or the best one if the limit was reached).
* `final_score` — the RAIL score of that final content, and `passed` — whether it cleared the threshold.
* `iterations_taken` — how many regenerate-and-score rounds ran.

If the limit is reached without passing, you still get the best attempt in `content` with `passed: false`, so you can decide whether to serve it, fall back, or route to a human.

## Usage

Each iteration meters like a standalone evaluation, up to the iteration limit you set. If the first response already passes the threshold, only that evaluation is counted. See [Credits](/getting-started/credits) for details.

## What's next

<CardGroup cols={2}>
  <Card title="API Reference: Safe Regeneration" icon="code" href="/api-reference/safe-regeneration">
    Full parameter reference.
  </Card>

  <Card title="Python SDK: Safe Regeneration" icon="python" href="/sdk/python/safe-regeneration">
    Python SDK examples and options.
  </Card>

  <Card title="Concepts: Middleware" icon="layer-group" href="/concepts/middleware">
    Auto-regenerate via provider wrappers.
  </Card>

  <Card title="Policy Engine" icon="gavel" href="/concepts/policy-engine">
    Trigger regeneration automatically from your application policy.
  </Card>
</CardGroup>
