Agentic AI Notebook
RAG Engineering
Phase 3Module 22 of 22

Graph Retrieval

A graph you never query is a museum. Retrieval has to be bounded: unbounded walks explode tokens and can leak another tenant's subgraph. Mix with vector search when the seed entity is unclear.

You do not photocopy the whole org chart. You start at Acme and walk one or two boxes over — then hand those boxes to the writer.

Visual Workflows

Start here — scroll inside each diagram frame to explore, then use + / to zoom up to 200% if needed.

Overview

100%
Loading diagram...

Scroll inside the frame to explore · use + / − to zoom up to 200%

100%
Loading diagram...

Scroll inside the frame to explore · use + / − to zoom up to 200%

Typical retrieve

100%
Loading diagram...

Scroll inside the frame to explore · use + / − to zoom up to 200%

Identify entities in the question, walk a bounded neighborhood, verbalize.

Bound the walk

100%
Loading diagram...

Scroll inside the frame to explore · use + / − to zoom up to 200%

Unbounded traversal explodes tokens and leaks tenants.

Key Takeaways

  • 1.Graph retrieval starts from entities in the question, walks a bounded neighborhood (k-hop, typed edges), and serializes those paths as context for the LLM. Parse or link entities in the query to canonical nodes.
  • 2.Expand along allowed edge types (acquired, reports_to, located_in) with max hops and a hard node cap. Filter to the user's tenant subgraph.
  • 3.Serialize as readable triples or a short path list, plus optional source snippets. LLM-generated Cypher is powerful and dangerous — constrain it.
  • 4.Always cite which nodes and source docs you used.

Real Example

Scenario

'Who is the manager of the person who owns Project Orion?' Seed Project Orion, hop to owner, hop to manager, return that path — not the entire company graph.

What you would do

In RAG Engineering, apply Graph Retrieval to this scenario: 'Who is the manager of the person who owns Project Orion?' Seed Project Orion, hop to owner, hop to manager, return that path — not the entire company graph. Identify the inputs, run the technique, validate the output, and note one thing you would monitor in production.

Practice Task

Open the Code Walkthrough below and run it locally. Change one parameter related to Graph Retrieval (e.g. model, temperature, top_k, or tool name), observe the difference in output, and write 2–3 sentences explaining what changed.

Code Walkthrough

Highlighted lines show where Graph Retrieval happens in the code.

Graph Retrieval
1# Graph Retrieval — minimal example2from openai import OpenAI3
4client = OpenAI()  # create API client5
6# Ask the model to explain this topic7response = client.chat.completions.create(  # core API call for Graph Retrieval8    model="gpt-4o-mini",9    messages=[10        {"role": "system", "content": "You explain graph retrieval clearly."},11        {"role": "user", "content": f"What is graph retrieval?"},12    ],13    temperature=0,14)15print(response.choices[0].message.content)  # show output for debugging

Commands

Commands to Remember

  • Seed entity → k-hop neighbors → serialize as context
  • Cap hops and node count or the prompt explodes
  • Filter by edge type: acquired, reports_to, located_in
  • Always retrieve inside the user's tenant subgraph

Cheat Sheet

Quick recap

quick ref
  • Seed entity → k-hop → serialize
  • Cap hops and node count
  • Filter edge types + tenant
  • Cite node sources

Common Mistakes

  • No hop or node cap — prompt overflow
  • Walking the global graph without tenant filters
  • Returning raw IDs the LLM cannot verbalize