Autonomous Agent 'Existence' Store¶
Autonomous agents in 2026 are increasingly self-sustaining, managing their own compute budgets, interacting with crypto-wallets, and performing multi-platform actions without human intervention. To avoid amnesia and resource exhaustion, these agents need a "hard drive" for their state and financial history.
Objective¶
Build a persistent store for an autonomous agent that tracks its financial transactions (wallets), historical decisions, and tool usage across months of operation.
Prerequisites¶
- Deeplake SDK:
pip install deeplake - Agent framework:
pip install pydantic-ai python-dotenv - A Deeplake API token.
- An OpenRouter API key for embeddings.
Set credentials first
Complete Code¶
import os
import time
import requests
from deeplake import Client
# --- Embedding via OpenRouter (mistralai/codestral-embed-2505, ctx 8K) ---
OPENROUTER_API_KEY = os.environ["OPENROUTER_API_KEY"]
def embed(texts):
"""Generate embeddings using Mistral Codestral Embed via OpenRouter."""
res = requests.post(
"https://openrouter.ai/api/v1/embeddings",
headers={"Authorization": f"Bearer {OPENROUTER_API_KEY}"},
json={"model": "mistralai/codestral-embed-2505", "input": texts},
)
return [item["embedding"] for item in res.json()["data"]]
# 1. Setup
client = Client()
# 2. Log an Autonomous Cycle
# Tracks the agent's action, budget impact, and internal reasoning
reasoning = "Need 10 hours of fine-tuning for improved task planning."
reasoning_emb = embed([reasoning])[0]
print("Logging autonomous agent cycle...")
client.ingest("existence_logs", {
"timestamp": [time.time()],
"action": ["Purchased H100 compute time"],
"cost_usd": [45.50],
"remaining_budget": [1205.00],
"reasoning": [reasoning],
"embedding": [reasoning_emb],
})
# 3. Decision Recall: Avoiding Repeated Mistakes
# Agent checks its history for similar past reasoning before acting
query_text = "purchase H100 compute"
query_emb = embed([query_text])[0]
# Hybrid Search combining reasoning vectors and keyword matches
emb_pg = "{" + ",".join(str(x) for x in query_emb) + "}"
similar_decisions = client.query("""
SELECT action, reasoning, remaining_budget,
(embedding, reasoning)::deeplake_hybrid_record <#>
deeplake_hybrid_record($1::float4[], $2, 0.5, 0.5) AS score
FROM existence_logs
ORDER BY score DESC
LIMIT 3
""", (emb_pg, query_text))
for log in similar_decisions:
print(f"Past Reasoning: {log['reasoning']} (Remaining: ${log['remaining_budget']})")
# 4. Global Budget Check
last_entry = client.query("SELECT remaining_budget FROM existence_logs ORDER BY timestamp DESC LIMIT 1")
if last_entry:
print(f"Current Wallet Balance: ${last_entry[0]['remaining_budget']}")
# Requires: export DEEPLAKE_API_KEY="..." (see quickstart)
# Requires: export DEEPLAKE_ORG_ID="your-org-id"
API_URL="https://api.deeplake.ai"
WORKSPACE="agent-007"
# --- Embedding via OpenRouter (mistralai/codestral-embed-2505, ctx 8K) ---
# Requires: export OPENROUTER_API_KEY="..."
# Helper: get embedding from OpenRouter
# Usage: EMBEDDING=$(get_embedding "your text here")
get_embedding() {
curl -s "https://openrouter.ai/api/v1/embeddings" \
-H "Authorization: Bearer $OPENROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d "{\"model\": \"mistralai/codestral-embed-2505\", \"input\": [\"$1\"]}"
}
# 1. Create existence logs table
curl -s -X POST "$API_URL/workspaces/$DEEPLAKE_WORKSPACE/tables/query" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $DEEPLAKE_API_KEY" \
-H "X-Activeloop-Org-Id: $DEEPLAKE_ORG_ID" \
-d '{
"query": "CREATE TABLE IF NOT EXISTS \"'$DEEPLAKE_WORKSPACE'\".\"existence_logs\" (id BIGSERIAL PRIMARY KEY, timestamp FLOAT8, action TEXT, cost_usd FLOAT4, remaining_budget FLOAT4, reasoning TEXT, embedding FLOAT4[]) USING deeplake"
}'
# 2. Log a cycle (insert with parameterized values)
# First, get the embedding for the reasoning text
REASONING="Need 10 hours of fine-tuning for improved task planning."
REASONING_EMB=$(get_embedding "$REASONING" | jq -c '.data[0].embedding' | tr '[]' '{}')
curl -s -X POST "$API_URL/workspaces/$DEEPLAKE_WORKSPACE/tables/query" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $DEEPLAKE_API_KEY" \
-H "X-Activeloop-Org-Id: $DEEPLAKE_ORG_ID" \
-d '{
"query": "INSERT INTO \"'$DEEPLAKE_WORKSPACE'\".\"existence_logs\" (timestamp, action, cost_usd, remaining_budget, reasoning, embedding) VALUES ('$(date +%s)', $1, 45.50, 1205.00, $2, $3::float4[])",
"params": [
"Purchased H100 compute time",
"'"$REASONING"'",
"'"$REASONING_EMB"'"
]
}'
# 3. Decision Recall via Hybrid Search
SEARCH_TEXT="purchase H100 compute"
SEARCH_EMB=$(get_embedding "$SEARCH_TEXT" | jq -c '.data[0].embedding' | tr '[]' '{}')
curl -s -X POST "$API_URL/workspaces/$DEEPLAKE_WORKSPACE/tables/query" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $DEEPLAKE_API_KEY" \
-H "X-Activeloop-Org-Id: $DEEPLAKE_ORG_ID" \
-d '{
"query": "SELECT action, reasoning, remaining_budget, (embedding, reasoning) <#> deeplake_hybrid_record($1::float4[], $2, 0.5, 0.5) AS score FROM \"'$DEEPLAKE_WORKSPACE'\".\"existence_logs\" ORDER BY score DESC LIMIT 3",
"params": [
"'"$SEARCH_EMB"'",
"'"$SEARCH_TEXT"'"
]
}'
# 4. Budget check
curl -s -X POST "$API_URL/workspaces/$DEEPLAKE_WORKSPACE/tables/query" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $DEEPLAKE_API_KEY" \
-H "X-Activeloop-Org-Id: $DEEPLAKE_ORG_ID" \
-d '{
"query": "SELECT remaining_budget FROM \"'$DEEPLAKE_WORKSPACE'\".\"existence_logs\" ORDER BY timestamp DESC LIMIT 1"
}'
Step-by-Step Breakdown¶
1. The Multi-Workspace Pattern¶
For autonomous agents, it's recommended to give each agent its own workspace_id. This acts as an isolated sandbox for its data, preventing "cross-agent interference" while allowing a master "governance" client to query across workspaces.
2. Decision Logic with BM25 + Vector¶
By indexing the reasoning and action columns, the agent can use:
- BM25 to find exact previous interactions with a specific wallet address or tool.
- Vector Search to find general concepts, such as "Is this a high-risk financial decision?" based on similar past reasoning.
3. Financial Auditability¶
Because every cycle is stored in a Deeplake managed table, the agent's actions are fully auditable. You can export these logs into a Dataset object at any time for compliance or human review.
What to try next¶
- Agent Memory (Evolving Brain): simpler memory patterns for chat agents.
- Massive Ingestion: handling logs from a swarm of 10,000+ agents.
- Tables Guide: advanced SQL filtering for logs.