Skip to main content
A minimal guide to agent session traces: decorate a function with cognee.agent_memory(save_session_traces=True), call it a couple of times, then recall what happened — including calls that raised an error.

Before You Start

  • Complete Quickstart to understand basic operations
  • Ensure you have LLM Providers configured — the initial remember() call needs one, even though tracing itself does not
  • Read Persistent Memory Across Sessions first — this guide assumes you already know what session_id is and how it works

Code in Action

What Just Happened

Step 1: Bootstrap Cognee and Decorate the Function

The remember() call runs once, before anything else, purely to make sure Cognee’s database and default user exist — the decorated function itself never reads this fact back, because with_memory=False. save_session_traces=True is what turns tracing on — without it, the decorator would still run the function but record nothing. with_memory=False and with_session_memory=False keep this example focused on traces alone: no graph memory lookup, no conversation-history retrieval, just “what happened when this function ran.” session_trace_summary=False skips the LLM-generated summary Cognee would otherwise attempt for each trace.

Step 2: Call It Once Successfully

"Alice" is in statuses, so the function returns normally. Behind the scenes, the decorator records this call’s trace with status="success" and the returned value — automatically, without you constructing any trace object yourself.

Step 3: Call It Again With an Error

"Bob" is not in statuses, so the function raises ValueError. The decorator still records this call’s trace — this time with status="error" and the error message — then re-raises the exception, which is why the call is wrapped in try/except here.

Step 4: Recall the Traces

scope="trace" tells recall() to search recorded traces instead of the knowledge graph or conversation history. Trace search matches by keyword across each trace’s function name, parameters, return value, and error message — query_text="Alice" matches the first call because "Alice" appears in its parameters. Each result exposes origin_function, status, method_params, method_return_value, and error_message, so both the successful call and the failed one are fully inspectable.

Step 5: Confirm Isolation Between Sessions

The same query against a session_id that never ran lookup_teammate_status returns an empty list — traces are scoped per session, just like the conversation history covered in Persistent Memory Across Sessions.

Traces vs. Other Session Concepts

A trace records one function call — its inputs, status, and outcome. Conversation history (covered in Persistent Memory Across Sessions) records question/answer turns from recall(). Both live in the same session cache and are scoped by session_id, but they answer different questions: “what did this code do?” vs. “what did we discuss?”
This guide keeps with_memory=False and with_session_memory=False so the decorated function never reads graph memory or session history back — it only writes traces. Enabling those flags (as examples/guides/agent_memory_quickstart.py does) lets a decorated function use memory during its own execution, which is a separate, more advanced concept from recording that the call happened.
Traces recorded here stay in the session cache — this guide sets no option that writes them to the permanent knowledge graph. Two mechanisms bridge them: improve(session_ids=...), the on-demand path covered in Session Distillation, and persist_session_trace_after=N on the decorator itself, which memifies the recent traces into the graph every Nth trace (see Persistent Agent Memory Quickstart). Both are out of scope for this guide.

Persistent Memory Across Sessions

Learn the session-cache concept traces are built on

Persistent Agent Memory Quickstart

See traces combined with session memory and graph memory