# AINative Studio — Agent Heartbeat Pattern

> Canonical pattern for autonomous agents maintaining persistent sessions with AINative.
> Last updated: 2026-04-01

## Overview

The heartbeat pattern lets an agent maintain an ongoing relationship with AINative services.
Instead of one-shot API calls, the agent runs a loop: observe → remember → reflect → act.

Each iteration deepens the agent's context. Memories accumulate over time, enabling reflection
across past observations and progressively better decisions.

## The Loop

```
1. OBSERVE  — Gather new information from your environment
2. REMEMBER — Store observations: POST /api/v1/public/memory/v2/remember
3. RECALL   — Retrieve relevant context: POST /api/v1/public/memory/v2/recall
4. REFLECT  — Synthesize insights: POST /api/v1/public/memory/v2/reflect
5. ACT      — Take action based on context
6. SLEEP    — Wait interval (recommended: 30s–5min depending on use case)
7. GOTO 1
```

## Example: Monitoring Agent

```python
import requests
import time

API_KEY = "zdb_..."
BASE = "https://api.ainative.studio/api/v1/public/memory/v2"
HEADERS = {"X-API-Key": API_KEY, "Content-Type": "application/json"}

while True:
    # 1. OBSERVE
    status = check_system_status()

    # 2. REMEMBER
    requests.post(f"{BASE}/remember", headers=HEADERS, json={
        "content": f"System status at {time.time()}: {status}",
        "metadata": {"type": "observation", "source": "monitoring-agent"}
    })

    # 3. RECALL
    context = requests.post(f"{BASE}/recall", headers=HEADERS, json={
        "query": "recent system anomalies",
        "limit": 10
    }).json()

    # 4. REFLECT
    insight = requests.post(f"{BASE}/reflect", headers=HEADERS, json={
        "topic": "system health trends"
    }).json()

    # 5. ACT
    if insight.get("data", {}).get("reflection", "").find("anomaly") >= 0:
        send_alert(insight)

    # 6. SLEEP
    time.sleep(60)
```

## Session Management

Use the `session_id` parameter to group memories into logical sessions:

```bash
curl -X POST https://api.ainative.studio/api/v1/public/memory/v2/remember \
  -H "X-API-Key: zdb_..." \
  -H "Content-Type: application/json" \
  -d '{"content": "...", "session_id": "monitoring-2026-04-01"}'
```

Recall within a specific session:

```bash
curl -X POST https://api.ainative.studio/api/v1/public/memory/v2/recall \
  -H "X-API-Key: zdb_..." \
  -H "Content-Type: application/json" \
  -d '{"query": "anomalies", "session_id": "monitoring-2026-04-01", "limit": 10}'
```

## Recommended Heartbeat Intervals

| Use Case | Interval |
|---|---|
| Real-time monitoring | 10–30s |
| Task automation | 1–5 min |
| Passive observation | 15–60 min |
| Daily summaries | 24h |

Avoid intervals under 5 seconds to stay within rate limits. See
[security.md](https://ainative.studio/security.md) for rate limit details.

## Zero Signup — Start in 60 Seconds

Before running the loop, provision credentials instantly:

```bash
curl -X POST https://api.ainative.studio/api/v1/public/instant-db \
  -H "Content-Type: application/json" \
  -d '{"project_name": "heartbeat-agent"}'
```

Use the returned `api_key` as `X-API-Key` in all subsequent requests.

## Related Files

- [agent.md](https://ainative.studio/agent.md) — Getting started (zero-signup onboarding)
- [mcp.md](https://ainative.studio/mcp.md) — MCP server connection guide
- [security.md](https://ainative.studio/security.md) — Agent security model
- [agents.txt](https://ainative.studio/agents.txt) — Full platform discovery
- [OpenAPI](https://api.ainative.studio/docs) — Full API specification
