Skip to main content
Use memory to persist state between run cycles within a session — for example, storing the last rebalance timestamp to avoid trading too frequently, tracking cumulative PnL, or caching external API responses. Access memory methods via agent.memory.

Set Value

Store a key-value pair.
def set(key: str, value: str) -> MemorySetResponse
Parameters:
  • key (string): Unique identifier (1-255 characters)
  • value (string): String value to store
Response:
  • success (boolean): Whether the operation succeeded
  • data.key (string): The key that was set (on success)
  • error (string | null): Error message (on failure)
Example:
result = agent.memory.set("lastSwapNetwork", "ethereum:42161")
if result.success and result.data:
    agent.log(f"Stored: {result.data.key}")

Get Value

Retrieve a value by key.
def get(key: str) -> MemoryGetResponse
Parameters:
  • key (string): The key to retrieve
Response:
  • success (boolean): Whether the operation succeeded
  • data.key (string): The requested key (on success)
  • data.value (string): The stored value (on success)
  • error (string | null): Error message if key not found
Example:
result = agent.memory.get("lastSwapNetwork")
if result.success and result.data:
    agent.log(f"Network: {result.data.value}")
else:
    agent.log(f"Key not found: {result.error}")

Delete Value

Remove a key-value pair.
def delete(key: str) -> MemoryDeleteResponse
Parameters:
  • key (string): The key to delete
Response:
  • success (boolean): Whether the operation succeeded
  • data.key (string): The deleted key (on success)
  • error (string | null): Error message (on failure)
Note: Succeeds even if the key doesn’t exist. Example:
result = agent.memory.delete("tempSwapQuote")
if result.success and result.data:
    agent.log(f"Deleted: {result.data.key}")

List Session Memory Keys

List all keys in session memory.
def list() -> MemoryListResponse
Response:
  • success (boolean): Whether the operation succeeded
  • data.keys (string[]): Array of all stored keys (on success)
  • data.count (number): Number of keys (on success)
  • error (string | null): Error message (on failure)
Example:
result = agent.memory.list()
if result.success and result.data:
    agent.log(f"Found {result.data.count} keys:")
    for key in result.data.keys:
        agent.log(f"  - {key}")

Notes

  • Values must be strings. Serialize complex data (JSON, numbers) before storing.
  • Keys must be 1-255 characters.
  • Keys are automatically scoped to the current agent session.
  • Memory persists across execution cycles within the same session.
  • Storage is cleared when the session ends.
  • delete() is idempotent — it succeeds even if the key does not exist.
  • Memory is backed by object storage (R2). Reads and writes have low but non-zero latency — avoid calling memory methods in tight loops.
  • For tracking position changes within an execution cycle, memory is often more reliable than polling getCurrentPositions(). See Positions for details.

See Also

  • Positions — Use memory to track position deltas instead of polling
  • Execution Model — How the run loop and session lifecycle work