DocumentationOpen App

The Agent Loop

The agent loop is a structured 6-step cycle for AI-powered capital management on Gearbox. Each stage has clear inputs, outputs, and decision points.

StepPurposeWho Decides
DiscoverScan all current opportunitiesSDK returns data
AnalyzeDue diligence on shortlisted candidatesAgent reasons over data
ProposeFind optimal rebalance — or decide to do nothingAgent + Router
PreviewVerify on-chain feasibility right nowSDK simulates
ExecuteSign and submitUser or agent via bot API
MonitorReact to events that require faster responseSDK returns live state

Step 1: Discover

Scan all available opportunities across chains. This is a broad survey — every pool, every strategy, every chain. The data is lightweight: headline APY, TVL, access requirements.

TypeScript
const opportunities = await sdk.opportunities.search({ chainIds: ["Mainnet", "Monad"], types: ["pool", "strategy"], assets: [Asset.STABLE], });

The agent applies its own filters (APY floor, TVL minimum, permissionless-only, etc.) and produces a shortlist of candidates for deeper analysis.

Step 2: Analyze

Due diligence on the shortlisted candidates. The agent inspects each one in detail using specialized research sub-agents:

Sub-AgentWhat It Evaluates
Curator ResearchGovernance quality, bad debt history, track record
Token ResearchCollateral liquidity, oracle reliability, risk classification
Profitability ForecastAPY sustainability, yield type (organic vs incentivized), trend
Risk Scoring5 dimensions: collateral, curator, smart contract, market, exit
Final RankingfinalScore = adjustedApy * (1 - overallRisk)
TypeScript
// Deep dive into a candidate const detail = await sdk.pools.getDetail({ chainId, address: poolAddress }); const curator = await sdk.curators.getProfile(detail.curatorId); const apyHistory = await sdk.history.getMetric({ target, metric: "apy", periodDays: 30 }); const events = await sdk.events.getFeed({ chainId, target, sinceDays: 30 });

The output is AnalyzedOpportunity[] — ranked by score, each with profitability forecasts, risk breakdowns, and reasoning.

Step 3: Propose

From the analyzed candidates, find the optimal action — or decide that no rebalance is needed.

The agent considers:

  • Is the current position already optimal?
  • Would the rebalance cost (gas, slippage) exceed the expected gain?
  • What's the best route for the chosen strategy?

If a rebalance makes sense:

TypeScript
// Query router for optimal path const route = await sdk.router.findOpenStrategyPath({ chainId, creditManagerAddress, collateralToken, collateralAmount, debtAmount, targetToken, slippageBps: 50, }); // Build the unsigned transaction const tx = await sdk.accounts.openCreditAccount({ chainId, creditManagerAddress, collateralToken, collateralAmount, debtAmount, pathCalls: route.calls, slippageBps: 50, });

If no action is needed, the agent skips to Monitor.

Step 4: Preview

Verify that the transaction is feasible on-chain right now. LLMs are not instant — by the time the agent finishes reasoning, on-chain conditions may have changed (liquidity exhausted, price moved, quota filled).

TypeScript
const preview = await sdk.previewTransaction(tx);

The preview simulates the exact bytes against current chain state and returns:

  • success — would this execute?
  • healthFactor — projected HF after entry
  • actions[] — human-readable step descriptions
  • balanceChanges[] — net token movements
  • warnings[] — concerns (stale oracle, high utilization, etc.)

If preview fails → back to Propose

The agent does not re-analyze. The due diligence is still valid — the candidates are sound. Only the execution parameters need adjustment:

  • Liquidity exhausted → reduce position size or try different route
  • Price moved → adjust slippage tolerance
  • Quota filled → switch to the next-ranked candidate from Analyze
TypeScript
if (!preview.success || (preview.healthFactor ?? 0) < 1.4) { // Adjust parameters and re-propose // Do NOT re-run Analyze — the research is still valid }

Step 5: Execute

Sign and submit the transaction. The SDK never signs — execution is the caller's responsibility.

Two paths:

  • Human-in-the-Loop — agent encodes preview as URL for verify.gearbox.finance, human reviews and signs
  • Bot Execution — agent signs via bot API with bounded on-chain permissions
TypeScript
const txHash = await walletClient.sendTransaction({ to: tx.to, data: tx.calldata, value: tx.value, });

Step 6: Monitor

Watch the live position and react to events. The monitor waits for signals — either scheduled (cron) or event-driven (alerts, external data).

TypeScript
const position = await sdk.accounts.getStatus({ chainId, creditAccount }); const alerts = await sdk.monitor.getAlerts({ chainId, creditAccount });

When events trigger a review → back to Analyze

The monitor doesn't jump to Propose — it goes back to Analyze to re-evaluate the situation with fresh data. This is critical: if a collateral token is dropping, the agent needs to understand the severity before deciding what to do.

EventUrgencyResponse
Scheduled check (cron, every 4h)NormalFull Analyze → Propose cycle
Collateral price droppingElevatedQuick Analyze → Propose adjustment
Critical event (hack, depeg, tweet)ImmediateEmergency exit via Propose → Preview → Execute
Better opportunity detectedNormalAnalyze alternatives → Propose migration
HF approaching liquidationUrgentAnalyze → Propose collateral top-up or debt reduction

The urgency of the trigger determines the pace. A cron check runs the full cycle. A critical event skips straight to action — but still passes through Analyze to confirm the threat is real before executing.

Learn More

  • MCP Server — MCP tools organized by loop stage
  • Execution — How agents interact with the protocol safely