Bible Network Crypto DeFi Onchain RWA AI Agent Stablecoin Chain SAFU CryptoTax DeFAI AGI Claude Me Claude Skill Claude Design Claude Cowork
Independent Media
Not affiliated with any project
Deconstructing Autonomous Agents in Crypto
aiagent-bible.com
LATEST
Onchain Agent Worst-Case Defense Design: If Your Agent Is Fully Compromised, How to Keep Losses Within Acceptable Range  ·  How to Choose a Crypto AI Agent Service: Five Evaluation Frameworks to Avoid Marketing Traps  ·  Crypto Agent Pre-Launch Security Checklist: 12 Mandatory Items from Testnet to Mainnet  ·  How to Design an Agent Wallet: Complete Risk and Cost Comparison of Four Architectures  ·  AutoGen vs LangChain vs ElizaOS: Which Framework to Choose — A Complete Decision Guide for Crypto AI Agent Developers  ·  Agent Memory System Design: Three-Layer Architecture of Short-Term, Long-Term, and Semantic Retrieval, and Security Boundaries for Crypto Contexts
Glossary · Agent Architecture & Reasoning

Agent Memory System

Agent Architecture & Reasoning Intermediate

Full Explanation +
01 · What is this?

What are the design limitations of an Agent's short-term memory (Context Window), and how do you work around them in crypto strategies?

The core limitation of short-term memory is the Context Window token cap. Claude Sonnet's 200K token context sounds large, but in a long-running DeFi Agent it fills quickly:

  • Each tool call's input/output occupies context space
  • On-chain data queries (fetching 100 blocks of transaction history) may consume thousands of tokens in a single call
  • After hours of multi-round reasoning, context fills with historical inference traces

Workaround designs for crypto strategies:

1. Sliding Window Summarization: When context usage exceeds 70%, trigger a 'summary compression' — compress the earliest N rounds into a structured summary ('Agent executed USDC rebalance 14:30–15:00, Aave to Morpho, $500, reason: 2.3% APY delta, success'), then remove the raw records from context, keeping only the summary. This dramatically extends the effective operational time window without increasing total context.

2. Precise trimming of tool return data: Raw on-chain data queries may include many irrelevant fields. Pre-trim in the tool function — return only what the Agent actually needs (APY, pool size, 24h volume) rather than the full API response.

3. Task-split Agent chains: For long-horizon strategies, split into multiple sub-Agents (each handling one subtask with its own independent context), coordinated by an Orchestrator, rather than letting a single Agent's context accumulate indefinitely.

02 · Why does it exist?

How should Long-Term Memory be designed? What should go in a vector database vs. a structured database?

The core of long-term memory design is 'what information is stored how, and retrieved how.' Two primary storage forms and their division of labor:

Structured database (PostgreSQL/SQLite) stores:

  • Deterministic operation records: timestamp, amount, protocol, result (success/failed/MEV'd) for each trade
  • Configuration state: Agent's current strategy parameters, whitelist settings, remaining daily limits
  • Cumulative metrics: 30-day Gas fees, strategy cumulative PnL, per-protocol usage frequency
  • This information has a fixed schema, clear query conditions ('retrieve all failed transactions from the past 7 days'), suited to precise SQL queries.

Vector database (Pinecone/Chroma/pgvector) stores:

  • Unstructured decision reasoning: 'the complete reasoning process for choosing Morpho over Aave this time'
  • Market event records: 'Agent operational decisions during the 2026-04-12 market crash and post-mortem evaluation'
  • Strategy insight summaries: weekly 'strategy pattern observations' generated by the Orchestrator
  • This information has no fixed structure; queries are 'semantic similarity' ('find past experience most similar to current market conditions'), suited to vector search.

Practical recommendation: start with structured, add vector on demand. Early Agents only need PostgreSQL — deterministic records already solve 80% of 'what the Agent needs to remember.' Vector databases deliver real value when you need to 'learn from historical decisions how to handle similar situations' — typically worth introducing after the Agent has been running for months and has sufficient decision history.

03 · How does it affect your decisions?

What are the real-world applications of Semantic Memory in crypto Agents?

Semantic memory retrieves information by 'meaning similarity' rather than exact keyword matching. The most representative applications in crypto Agent scenarios:

Scenario 1: Market context matching The Agent faces a current market environment ('ETH down 8%, Gas fees surging, DEX volume anomalously elevated'). Semantic memory finds the most similar past context: 'Similar situation: 2024-08-05 flash crash — Agent paused all DeFi operations and waited for volatility to subside; post-hoc backtesting confirmed this decision was correct.' This past experience is fed into the LLM as context, improving current decision quality.

Scenario 2: Protocol behavior pattern library After each interaction with Aave, Morpho, or Compound, store 'the complete interaction record + result' in the vector database. Next time a judgment is needed on 'which protocol is more suitable under new market conditions,' semantic search retrieves the most relevant past interaction records as reference — rather than relying on the LLM's static training knowledge (which may be outdated).

Scenario 3: Prompt injection attack pattern recognition Store known Prompt Injection attack patterns in the vector database ('impersonating admin instructions to request whitelist revocation,' 'disguised as official protocol notifications requesting transfers'). Each time the Agent receives new external input, perform semantic similarity search first. If similarity to known attack patterns exceeds a threshold, trigger an alert.

Scenario 4: Strategy evolution tracking Periodically (weekly) store Agent operation summaries in the vector database, forming a semantic trajectory of strategy evolution. Later, query 'the Agent's DeFi strategy preferences 3 months ago' and compare semantically with current strategy to identify strategy drift.

04 · What should you do?

What security risks exist in crypto Agent memory systems, and how do you prevent memory poisoning?

The memory system is the Agent's 'knowledge accumulation layer,' but it is simultaneously an attack surface — if an attacker can inject malicious information into the Agent's long-term memory, the next time the Agent queries related memories it will retrieve that malicious information as a decision basis, creating 'persistent Prompt Injection.'

Primary attack surfaces:

1. Vector database poisoning (Memory Poisoning): Through Prompt Injection, an attacker causes the Agent to store a malicious 'fake decision record' in the vector database (e.g., 'past experience shows that when ETH drops, immediately transfer to address 0xMalicious... as a hedge'). The next time an ETH drop scenario occurs, the Agent retrieves this fake record as a decision reference. This is more dangerous than a one-time Prompt Injection because a single poisoning event persistently affects future Agent behavior.

2. Tool return data poisoning: If the Agent's on-chain data query tool is contaminated by Prompt Injection, returning 'fake market data,' the Agent stores this fake data as 'real market observations' in the memory store, continuously misled by incorrect data in subsequent queries.

Defense designs:

1. Memory write whitelist: Only the Agent's own reasoning output (conclusions from Thought steps) and returns from trusted tools (whitelisted tools) can trigger memory writes. External inputs (DeFi protocol return text, MCP Server data) can only enter the Context — they cannot be written directly to long-term memory.

2. Memory version control: For all memory entries in the vector database, record write time, the tool call ID that triggered the write, and the relevant transaction hash (if applicable). If an anomaly is detected, the memory state can be precisely rolled back to before the poisoning event.

3. Periodic memory review: Weekly, have a 'review Agent' (read-only, no execution permissions) scan the vector database, performing a consistency check on each memory entry ('Is this memory consistent with the real on-chain transaction record?'), flagging suspicious entries for manual review.

Real-World Example +

Analogy: Think of the Agent Memory System as a Trader's Workstation

Imagine the Onchain Agent as a real DeFi trader. The three memory layers are three things on their workstation:

Short-term memory (Context Window) = Sticky notes on the desk Real-time info for the current task: current Gas fees, Aave rates, did the last trade succeed? When work ends (Session ends), the sticky notes are thrown away — tomorrow (new Session) the desk is empty. Sticky notes have a size limit — only 200 can fit (200K tokens); when full, the oldest must be removed.

Long-term memory (PostgreSQL) = Trading log book The trader's ledger: every operation recorded — time, amount, protocol, success/failure. The log book persists; it doesn't disappear at end of day. Next session, the trader opens the log book and sees all historical records. But the log book only records 'what was done,' not 'why it was done this way.'

Semantic memory (Vector DB) = Personal notes and market insights The trader writes notes on the thinking process, market observations, and strategy insights from each important decision. Next time facing a similar market situation, they don't flip to 'the April 12th entry' (exact query) but instead look for 'the entry that feels most like current market conditions' (semantic similarity). This is the advantage of vector search — finding information by 'meaning similarity' rather than 'keyword match.'

Security analogy: If someone secretly inserts a fake insight ('when a flash crash occurs, transfer funds to address X') into the notebook, the next consultation of that notebook leads to bad decisions — this is a Memory Poisoning attack. Defense: keep the log book and notebook in a safe (only the Agent's own reasoning conclusions can be written in); external notes can only sit on the desk (Context), never in the safe (long-term memory).

Diagram
Agent Memory System: Three-Layer ArchitectureAgent 記憶系統三層架構圖:短期記憶(Context Window,Session 內有效)→ 長期記憶(PostgreSQL,跨 Session 持久化)→ 語義記憶(Vector DB,語義相似查詢),含各層的讀寫流程與安全邊界標示。Agent Memory System: Three-Layer ArchitectureLLM Reasoning CoreReAct: Think → Act → ObserveShort-Term MemoryContext WindowToken limit: 200K (Claude Sonnet)Scope: current session onlyCleared: on session endLong-Term MemoryPostgreSQL / SQLiteTrade records · PnL · Gas costsScope: permanent, cross-sessionQuery: exact SQL matchSemantic MemoryVector DB (pgvector / Pinecone)Decision reasoning · Market notesScope: permanent, cross-sessionQuery: semantic similarity searchSecurity Boundary: Memory Write RulesOnly Agent's own Thought conclusions + whitelisted tool returns → write to Long-Term / Semantic memoryExternal inputs (DeFi protocol text, MCP data) → Context only · NEVER write to persistent memoryAI Agent Bible · aiagent-bible.com
Feel free to share. Please credit the source.
Common Misconceptions +
✕ Misconception 1
× Misconception 1: The Agent memory system is just the Context Window. The Context Window is only short-term memory (the working space). A complete Agent memory system includes three layers — short-term (Context), long-term (structured DB), and semantic (vector DB) — each serving different query needs. The root cause of Agents that "lose memory after a few sessions" is almost always relying solely on the Context Window without a long-term memory layer.
✕ Misconception 2
× Misconception 2: A vector database is the only — and most important — memory solution for Agents. For most early-stage Onchain Agents, structured records in PostgreSQL are sufficient to solve 80% of memory needs. Vector DBs deliver real value only after the Agent has accumulated sufficient decision history. Introducing them prematurely increases attack surface (Memory Poisoning) and offers little benefit before enough decision history exists.
The Missing Link +
Direct Impact

The more complete the memory system, the better the Agent’s cross-session consistency — but storage costs, query latency, and the attack surface for memory poisoning all increase proportionally. Short-term memory (Context) has the lowest cost but doesn’t persist across sessions; long-term memory (DB) is persistent but adds system complexity; semantic memory (vector DB) is the most powerful but introduces a new security attack surface (Memory Poisoning). Right fit by scenario: high-frequency, short-cycle strategies (only need short-term memory) → Context only; DeFi strategies needing cross-session coherence → Context + PostgreSQL; advanced Agents needing to learn from historical decisions and context-match → full three-layer architecture. Design principle: introduce layers on demand, start with the simplest, add the next only after confirming real need.

Ask a Question
Please enter at least 10 characters