Build and run the agent

In the previous lesson, you learned how to use create_memory_tools() and record_agent_trace() to record reasoning traces automatically, and how to query those traces in Neo4j to follow the full causal chain of an agent’s decisions.

In this challenge, you will complete a pre-scaffolded Pydantic AI agent, run it against two test prompts, and verify that the reasoning trace was written to Neo4j.

Review the scaffold code

The repository includes a starter file at agent/memory_agent.py. It has the imports and settings in place — you need to connect the MemoryClient, add the memory tools, run two prompts, and record the traces.

python
agent/memory_agent.py — complete the TODOs to build the agent
# agent/memory_agent.py (starter — complete the TODOs)
import asyncio
import os
from neo4j_agent_memory import MemoryClient, MemorySettings
from neo4j_agent_memory.config import Neo4jConfig
from neo4j_agent_memory.integrations.pydantic_ai import create_memory_tools, record_agent_trace
from pydantic_ai import Agent

settings = MemorySettings(
    neo4j=Neo4jConfig(
        uri=os.environ["NEO4J_URI"],
        username=os.environ["NEO4J_USERNAME"],
        password=os.environ["NEO4J_PASSWORD"]
    )
)

SESSION_ID = "student_user"

async def main():
    # TODO 1: Open a MemoryClient using `async with`
    # TODO 2: Call create_memory_tools() and pass to Agent
    #         (sessions are created automatically — no add_session needed)
    # TODO 3: Run two prompts and call record_agent_trace() after each:
    #   Prompt 1: "My name is Alex and I prefer concise answers."
    #   Prompt 2: "What do you know about me?"
    #   For each: result = await agent.run(prompt)
    #             await record_agent_trace(memory.reasoning, SESSION_ID, result, task=prompt)
    pass

asyncio.run(main())

Verify your results

After running the agent, the following Cypher query should return at least one ReasoningTrace node:

cypher
Check that reasoning traces were written to Neo4j
MATCH (t:ReasoningTrace)
RETURN t.task, t.status, t.created_at
ORDER BY t.created_at DESC
LIMIT 5

The challenge verifier checks that your Neo4j instance contains the following node labels:

  • At least one ReasoningTrace node

  • At least one ReasoningStep node connected to that trace

  • At least one ToolCall node connected to a step

Lesson Summary

In this challenge, you connected a Pydantic AI agent to neo4j-agent-memory, ran it against two prompts, and verified that the complete reasoning trace — including ReasoningTrace, ReasoningStep, and ToolCall nodes — was written to Neo4j.

Chatbot

How can I help you today?

Data Model

Your data model will appear here.