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

# AI agent loop

> The self-improving AI loop that tunes the grid.

The AI agent loop is the part of Perps Agent that **picks the grid parameters** and gets better at it over time — in a way that is provable, not just claimed.

## The loop

<Steps>
  <Step title="Observe">
    Pull recent market state (price, volatility, funding, microstructure) plus the user's risk caps.
  </Step>

  <Step title="Recall">
    Read past *attested* runs on similar regimes from Mantle. Only attested runs count — backtests do not.
  </Step>

  <Step title="Propose">
    Generate candidate grid configs (range, step, count, sizing) that maximize a risk-adjusted score under the user's caps.
  </Step>

  <Step title="Commit">
    Hash the chosen config and write it to the Mantle commit registry. The agent is now locked to that config.
  </Step>

  <Step title="Trade">
    Hand control to the grid engine on Bybit.
  </Step>

  <Step title="Attest">
    At the end of the run, write the verified outcome on-chain, linked to the original commit.
  </Step>

  <Step title="Learn">
    The next iteration's "recall" step reads this new attestation. The loop closes.
  </Step>
</Steps>

## Why this matters

Most "learning" trading bots train on private data the user can't see. Perps Agent's training signal is the **public on-chain attestation history**. The same data anyone else can verify.

## What it optimizes

Not raw PnL. The objective is risk-adjusted: returns penalized by realized variance and drawdown, hard-clipped at the user's risk caps. A run that hits the drawdown cap is treated as a loss for learning purposes, even if it would have recovered.
