> For the complete documentation index, see [llms.txt](https://docs.neko.fun/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.neko.fun/architecture/ai-ml-architecture/cadence-mechanism.md).

# Cadence Mechanism

### What It Is

**Cadence** is a high-performance, Rust-based monitoring and alerting system designed for real-time DeFi protocol analysis. Its core function is to detect market anomalies, volatility spikes, and other critical events, and to proactively notify the Neko DeFAI Multiagent System.

It is **not** a passive data pipeline. Instead of providing raw market data, Cadence performs its own sophisticated analysis and sends structured, high-signal alerts only when specific, actionable conditions are met.

#### Your Benefit

Cadence acts as the nervous system for the Neko agent swarm. It handles the intensive, millisecond-grade task of monitoring market conditions, allowing the DeFAI Multiagent System to focus on high-level strategy, planning, and execution. This separation of concerns ensures the agent remains efficient and can react instantly to critical events without the overhead of constant market scanning.

{% hint style="info" %}
This monitoring/alerting system will be helpful and able to detect  and alert the multi-agent system for mass liquidation & cascading liquidation events (which can trigger ADL) both on HyperEVM and HyperCore.&#x20;
{% endhint %}

#### System Architecture

The integration between Cadence and the Neko agent is event-driven and uses a **push-based webhook model**, not polling or WebSockets. This ensures minimal latency from detection to action.

**Alert Workflow:**

1. **Continuous Monitoring**: The standalone **Cadence** system continuously ingests and analyzes real-time data from HyperEVM protocols (AMMs, lending venues) and the Hyperliquid order book. It uses specialized services like `Volatility Monitor`, `Anomaly Detector`, and `Health Score Monitor` to score market conditions.
2. **Anomaly Detection**: When a predefined condition is met (e.g., volatility spikes beyond a threshold, a pool's health score drops), Cadence generates a structured **`Alert` object**. This object contains the severity, alert type, and a snapshot of relevant `pool_metrics` at the moment of detection.
3. **Webhook Notification**: Cadence immediately sends the `Alert` object via an HTTP `POST` request to a dedicated endpoint on the agent's API server: `POST /webhook/cadence/alert`
4. **Alert Reception & Triage**: The agent's `handle_cadence_alert` function receives the alert and checks its `severity`.
   * **Non-Critical Alerts** (`Warning`, `Info`): The alert is logged for auditing and an acknowledgment is sent back to Cadence. No automated action is taken.
   * **Critical Alerts** (`Critical`): The agent immediately escalates the alert for processing by the DeFAI Multiagent System.
5. **Prompt Formatting**: For a critical alert, the agent transforms the structured JSON data into a natural language prompt, prefixed with **"URGENT ALERT from Cadence monitoring system"**. This prompt includes all the `pool_metrics` provided by Cadence, which the agent trusts as the real-time state of the market.

   *Example Formatted Query:*

   ```
   URGENT ALERT from Cadence monitoring system:
   Alert Type: volatility_spike
   Severity: critical
   Pool ID: pool_0xffaa4a3d97fe9107cef8a3f48c069f577ff76cc1
   Message: Extreme volatility detected in WHYPE/USDC pool - 85% spike

   Pool Metrics:
   - Price: $1.8505
   - Volume 24h: $1,500,000.00
   - Liquidity: $5,000,000.00
   - Volatility: 85.00%
   - Health Score: 3.20

   Please analyze this alert and determine if any immediate action is needed...
   ```
6. **Defensive Workflow Execution**: This urgent prompt is passed to the `run_agent_workflow`. The system's prompts (`PLANNER_SYSTEM_PROMPT`, `REPLANNER_PROMPT`, `ACTION_PLANNER_PROMPT`) are specifically enhanced to:

   * **Prioritize** these alerts over standard tasks.
   * Shift from yield optimization to **capital preservation**.
   * Generate a streamlined, time-sensitive plan that may involve immediate liquidity removal or range adjustments.
   * Execute actions with a **higher slippage tolerance** (e.g., 1-2%) to ensure transaction success in volatile conditions.

   <figure><img src="https://684504343-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy6KZK4gpJ1V4VuGXtQO5%2Fuploads%2FutnM9J6CK0LNq3npAFpm%2Fimage.png?alt=media&amp;token=efe8812f-a09c-47a0-8e9c-87449b8509e4" alt=""><figcaption></figcaption></figure>
