Skip to content

Module 02 · Lesson 2.2

Diagramming What the Code Does

45 minUpdated 19 Nov 2026Part of Architecture Recovery and Documentation · Existing Project Engineering
On this page
  1. What you will learn
  2. Context diagram and component diagram explained
  3. Worked scenario: Tidewell's two diagrams
  4. Checking every box against the code
  5. Using an AI assistant to draft a diagram
  6. Step-by-step workflow
  7. Finding where documents and code disagree
  8. Conclusion and your next step
  9. Reusable prompt
  10. Expected output
  11. Common errors and fixes
  12. Validation checklist
  13. Hands-on exercise
  14. Knowledge check
  15. Frequently asked questions
  16. References

A good map shows a visitor the main roads, and nothing that has been demolished. An old architecture diagram is often a map of a town that has changed. People trust it because it is a picture, and a picture looks sure of itself.

In this lesson you will draw two diagrams from evidence: a context diagram (the system and the world around it) and a component diagram (the parts inside). You will write them as text, so they live in the repository and can be reviewed. An AI assistant will draft them. You will check every box.

The example continues with Tidewell Wholesale and tidewell-orders, using the import graph from Components, Boundaries and Dependencies. The company, people and data are invented.

What you will learn

  • Explain the difference between a context diagram and a component diagram, and draw each.
  • Write a diagram as Mermaid text, with every box tied to a folder or module.
  • Mark unverified boxes and arrows so a reader can see what is a guess.
  • Check an assistant's draft box by box, and list where old documents and the code disagree.

Before you start

Context diagram and component diagram explained

One diagram cannot show everything. If you try, it becomes a tangle. So architects draw at different zoom levels. This lesson uses the first two levels of the C4 model (opens in a new tab), a popular way to organise diagrams. You do not need the whole model.

Context diagram
A picture of your system as one box, with the people and other systems around it. It is like a map of a shop and its customers and suppliers, with no shelves drawn. It answers: who uses this, and what does it talk to?
Component diagram
A picture of the parts inside the system, and which part needs which. It is like the floor plan of the shop: the counter, the stockroom, the office. It answers: where does each job live?
Mermaid
A way to write a diagram as plain text. You write the boxes and arrows, and a viewer draws the picture. Think of writing down a recipe and having a cook plate it. Many tools, such as GitHub, can show Mermaid in a Markdown file.
Unverified
A box or arrow that you saw in a note or an old document but could not find in code or config. You keep it, but you mark it, so nobody takes it for a fact.

Why text? A picture saved as an image cannot be reviewed line by line. A Mermaid diagram in docs/architecture.md changes in the same pull request as the code. A reviewer sees exactly which arrow was added.

Worked scenario: Tidewell's two diagrams

You have an import graph and a table of external dependencies. Now you put them into docs/architecture.md. Start with the context diagram. It shows who uses tidewell-orders and what it talks to.

Text
flowchart LR
  trade(["Trade customers"])
  edi(["EDI customers' systems"])
  support(["Support staff"])
  finance(["Finance and accounts team"])
  system["tidewell-orders"]
  email["Email provider (SMTP)"]
  db[("PostgreSQL")]
  redis[("Redis")]
  sched["Host scheduler (unverified)"]

  trade -->|"order through the portal"| system
  edi -->|"send orders"| system
  support -->|"replay orders from the command line"| system
  system -->|"order confirmations"| email
  system --> db
  system -->|"background job queue"| redis
  system -.->|"weekly CSV export (unverified)"| finance
  sched -.->|"nightly stock sync (unverified)"| system

Context diagram, as Mermaid text. In docs/architecture.md it sits inside a mermaid code block.

What each part does

  • flowchart LR starts a diagram that reads left to right. TB reads top to bottom.
  • trade(["Trade customers"]) makes a box with a short id (trade) and a label. Round ends mean a person or outside party.
  • db[("PostgreSQL")] makes a can shape, which is the usual sign for a database.
  • a -->|"text"| b draws an arrow from a to b with a label on it.
  • -.-> draws a dashed arrow. Here a dashed arrow, and the words (unverified), mark a link you could not confirm.

Each outside box has its evidence from the last lesson. The portal and EDI importer are in the code. The replay command is in orders/management/commands/. The two dashed links are open questions: the weekly export and the nightly sync.

Now the component diagram. This time every box names its folder or module, and the arrows come from your import graph.

Text
flowchart TB
  api["api/ (portal endpoints)"]
  orders["orders/ (create_order, tasks)"]
  replay["orders/management/commands/replay_orders.py"]
  dispatch["dispatch/ (EDI importer)"]
  customers["customers/"]
  invoicing["invoicing/ (price_order)"]
  legacy["legacy/pricing_v1.py"]
  catalogue["catalogue/"]
  stock["stock/"]
  worker["Celery worker (tidewell/celery.py)"]
  export["scripts/export_accounts.py (unverified)"]
  db[("PostgreSQL")]
  redis[("Redis")]
  email["Email provider (SMTP)"]

  api --> orders
  api --> customers
  dispatch --> orders
  dispatch --> customers
  replay --> orders
  orders --> invoicing
  orders --> stock
  orders --> customers
  invoicing --> legacy
  invoicing --> catalogue
  orders -->|"queues confirmation"| redis
  redis --> worker
  worker -->|"runs orders/tasks.py"| email
  orders --> db
  export -.->|"reads orders (unverified)"| db

Component diagram, as Mermaid text. Every box names a folder or module.

Compare the arrows with the import graph. The first ten are the nine edges plus the replay command. The next ones come from the links made by name. The last one is dashed: nobody has confirmed that the export script still runs.

Checking every box against the code

A diagram is a set of claims. 'The portal calls orders' is a claim. 'Redis feeds the worker' is a claim. Check each one the way you check a fact in a reading log: find the evidence, and write it down.

Box or arrowEvidence to findResult
api/ to orders/Import line in api/views.pyConfirmed in the import graph
legacy/pricing_v1.pyfrom legacy.pricing_v1 import ... in invoicing/totals.pyConfirmed. Not dead code.
Celery workertidewell/celery.py, worker command in the compose or deploy filesConfirmed
RedisCELERY_BROKER_URL in settings; redis in the compose fileConfirmed
Email providerEMAIL_HOST in production.pyConfirmed
replay_orders.pyFile exists; it calls create_orderConfirmed
Weekly CSV exportScript exists; no proof it runsUnverified. Ask the finance systems contact.
Host schedulerA hand-over note onlyUnverified. Ask the platform team lead.
Each row of the check is one claim and one piece of evidence.

If a box fails, do one of two things. Delete it, or keep it dashed and marked (unverified). Never leave a guess drawn as a solid line.

Using an AI assistant to draft a diagram

Drawing Mermaid by hand is slow. An assistant is good at that part. It turns your tables into Mermaid text in seconds. But it has not seen your repository, so you must treat its draft as a first guess.

Here is what an assistant might do if you give it only a folder tree and the old 2019 diagram. This is an illustrative reply, not a real run.

Illustrative: a draft built from folders and an old pictureIllustrative — your output will differ
flowchart TB
  api["api/"] --> orders["orders/"]
  orders --> pricing["Pricing service"]
  orders --> cache[("Redis cache")]
  orders --> stock["stock/"]
  dispatch["dispatch/"] --> orders

Check it box by box. Three things are wrong.

  • 'Pricing service' is gone. It came from the 2019 picture. Pricing is now legacy/pricing_v1.py, inside the same project, and invoicing calls it, not orders.
  • 'Redis cache' is a guess. The evidence says Redis is the message queue for Celery. No cache appears in settings.
  • Boxes are missing. The draft leaves out customers/, invoicing/, catalogue/, the worker, the email provider and PostgreSQL, because they were not in the input.

Better input gives a better draft. Give it the import graph, the external dependency table and the rules: one box per folder, dashed lines for unverified items, and a list of assumptions at the end.

Step-by-step workflow

  1. Step 1: Gather the evidence

    Collect your import graph, external dependency table, entry points and open questions. Note the commit hash. Remove secrets and personal data.

    What you should see: One page of evidence, with the commit hash.

  2. Step 2: Draw the context diagram first

    One box for the system. Add every person, outside system and service that touches it. Label each arrow with what flows along it.

    What you should see: A short diagram with 6 to 10 boxes.

  3. Step 3: Draw the component diagram

    One box per component, each naming a folder or module. Draw arrows from the import graph, then add the links made by name.

    What you should see: A diagram whose every box maps to code.

  4. Step 4: Draft with an assistant

    Give the assistant only your evidence and rules. Ask for Mermaid text, plus a list of assumptions. Or write the first version yourself and ask it to review.

    What you should see: A draft you can compare with your own.

  5. Step 5: Check box by box

    For each box and arrow, find the file or setting. Delete what you cannot find, or mark it (unverified) and dash it.

    What you should see: A diagram with every claim confirmed or marked.

  6. Step 6: Commit it with a date

    Save the text in docs/architecture.md with the verified commit and date. A reviewer reads it like code.

    What you should see: A reviewed diagram stored with the code.

From evidence to a checked diagram
  1. Evidence

    Graph, config, entry points

  2. Context diagram

    System and its world

  3. Component diagram

    Boxes named by folder

  4. Assistant draft

    From evidence only

  5. Box-by-box check

    Confirm or mark

  6. Commit with stamp

    Hash and date

The assistant helps with drafting. Checking is your step.

Finding where documents and code disagree

While you check boxes, you will meet old documents that say something else. Do not fix them in silence. List each disagreement. The list is a deliverable of this module, and it protects the next reader.

Document saysCode saysEvidenceWhat to do
README: Python 3.8Python 3.11 or newerrequires-python in pyproject.tomlUpdate the README, or point it to pyproject.toml.
docs/architecture-2019.png shows a separate 'pricing service'It is legacy/pricing_v1.py, a module in the same projectinvoicing/totals.py imports itMark the picture as out of date.
The same picture has no EDI importerdispatch/importers.py creates orders from EDIcreate_order(..., source="edi")Add it to the component diagram.
README does not mention Celery or RedisCelery workers use Redis as the brokertidewell/celery.py, CELERY_BROKER_URLAdd both to the README's run instructions.
The disagreements list for tidewell-orders.

Each row has a column for evidence, so a colleague can check it. Each row also says what to do. Often the answer is 'mark it as out of date', not 'rewrite it today'.

Conclusion and your next step

You now know how to draw two diagrams from evidence, keep them as text, mark what is unverified, and check an assistant's draft box by box. You also have a list of where documents and code disagree.

Your next step: diagrams show what exists, not why. Continue with Recovering Design Decisions to find out the reasons behind the shapes you just drew.

For more, read Mapping Entry Points and Data Flow for the paths behind the arrows, and the program overview Existing Project Engineering.

Reusable prompt

Mermaid diagram drafter

Drafts a context diagram and a component diagram in Mermaid from your evidence, with unverified items dashed and assumptions listed.

Draft two Mermaid flowcharts for a Python and Django service called tidewell-orders (fictional), using ONLY the evidence below.

Evidence:
- Import graph: api->orders, api->customers, orders->invoicing, orders->stock, orders->customers, invoicing->legacy, invoicing->catalogue, dispatch->orders, dispatch->customers.
- orders/management/commands/replay_orders.py calls create_order (used by support).
- Celery worker in tidewell/celery.py; Redis is the broker (CELERY_BROKER_URL); orders/tasks.py sends confirmation emails through SMTP (EMAIL_HOST in production.py).
- PostgreSQL (DATABASES in settings).
- Trade customers use the portal (api/); larger customers send EDI orders (dispatch/importers.py).
- scripts/export_accounts.py exists, but nobody has confirmed the weekly export to the accounts team is still used.
- A nightly stock sync from the host scheduler is mentioned in a note, with no code or config found.

Diagram 1: a context diagram (flowchart LR) with the system as one box, the people and outside systems around it.
Diagram 2: a component diagram (flowchart TB) where every box names its folder or module.
Rules: use a dashed arrow (-.->) and the words (unverified) for anything the evidence does not prove; do not add boxes that are not in the evidence; keep labels short. After the diagrams, list every assumption you made.
Show the reusable template

Replace each {{placeholder}} with your own details.

Draft two Mermaid flowcharts for {{system_description}}, using ONLY the evidence below.

Evidence:
{{evidence_list}}

Diagram 1: a context diagram (flowchart LR) with the system as one box, the people and outside systems around it.
Diagram 2: a component diagram (flowchart TB) where every box names its folder or module.
Rules: use a dashed arrow (-.->) and the words (unverified) for anything the evidence does not prove; do not add boxes that are not in the evidence; keep labels short. After the diagrams, list every assumption you made.

Expected output and limitations

Two Mermaid blocks and a list of assumptions. Every box in the component diagram names a folder or module. Unproven links are dashed and say (unverified). The assumptions list is short and honest.

Sample responseIllustrative — your output will differ
flowchart TB
  api["api/ (portal endpoints)"]
  orders["orders/ (create_order, tasks)"]
  invoicing["invoicing/ (price_order)"]
  legacy["legacy/pricing_v1.py"]
  api --> orders
  orders --> invoicing
  invoicing --> legacy
  export["scripts/export_accounts.py (unverified)"] -.-> db[("PostgreSQL")]

Assumptions
- I assumed the export script reads the database. Nothing in the evidence says so.
- I did not draw a cache, because the evidence only shows Redis as a queue.
- (unverified) The nightly stock sync is shown dashed in the context diagram.

Limitations to keep in mind

  • The assistant has not seen the repository. It can only arrange the evidence you pasted.
  • It may draw a typical box that is not in the evidence. Compare each box with your list.
  • Mermaid text can look right and still fail to render. Open it in a viewer.
  • A tidy picture is not a true picture. The box-by-box check is still yours.

Common errors and how to fix them

Drawing from the folder tree or an old diagram

Why it happens: Both are easy to find, and neither shows how the parts are wired today.

Fix: Draw from your import graph and config evidence. Use old diagrams only to generate questions.

Boxes that name no folder or module

Why it happens: A box such as 'Business logic' feels clear to the author but cannot be checked.

Fix: Rename it to a folder or module, or take it out until you can.

Guesses drawn as solid arrows

Why it happens: The author forgot, or an assistant removed the markers.

Fix: Dash every unconfirmed arrow, add (unverified), and keep a list of open questions.

Validation checklist

Tick each item once you have confirmed it.

Hands-on exercise

Diagram your own repository

Draw a context diagram and a component diagram for a repository you may study. Draft them with an assistant if policy allows, then check every box. List where the documents and code disagree.

You will need

  • Your import graph, external dependency table and open questions from the previous lesson.
  • Any old architecture document or README for the repository.
  • A Mermaid viewer, or a Markdown tool that renders Mermaid.

Acceptance criteria

Your work is done when every one of these is true.

Hints (3)
  • Start from the system and its outside world, then zoom in. Do not start with the folders.
  • If an assistant adds a box you cannot find, ask yourself where it saw it. Often it came from an old document or a guess.
  • If you find no disagreements, check the README's run steps and version numbers against the config files.
Show a model answer

A strong answer opens with a commit hash and has two small diagrams. The context diagram has the users, the outside services and the system, with one or two dashed unverified links. The component diagram shows every folder as a box, with arrows taken from the import graph. A table beside it gives the evidence for each box. The assistant's draft is kept, with a note of what was deleted and why. The disagreements list gives file names. A weak answer has boxes like 'Backend' and 'Services', no evidence, and solid arrows for things nobody checked.

Knowledge check

Choose an answer, then check it. Nothing is recorded — this is for you.

  1. 1.What does a context diagram show?
    Show the answer and explanation

    Answer: A. A context diagram is the outside view: who uses the system, and what it talks to. Classes and functions belong to a much closer zoom level. File history is not part of the picture.

  2. 2.An assistant's component diagram includes a 'Pricing service' box. The only place that mentions it is the 2019 picture. What should you do?
    Show the answer and explanation

    Answer: B. The old diagram is a source of questions, and the code is the evidence. Keeping the box because the team drew it is the tempting answer, but the team drew a system that has since changed. The assistant cannot see your code, so asking it only repeats the guess.

  3. 3.You cannot find any code that runs the weekly CSV export. How should the diagram show it?
    Show the answer and explanation

    Answer: C. Not finding it is not proof that it does not exist: it may run elsewhere. A solid arrow would claim it is confirmed. A dashed, marked link keeps the question visible until someone answers it.

Frequently asked questions

What is Mermaid, and do I need to install it?

Mermaid is a way to write diagrams as text. You do not need to install it to write the text. To see the picture, use a viewer or a tool that renders it, such as a Markdown preview that supports Mermaid.

How many boxes should a diagram have?

Aim for about 6 to 15. If you need more, split the picture into two. A reader should understand it in a minute. A diagram that tries to show everything shows nothing.

Can I trust a diagram my AI assistant draws?

Not by itself. It is a draft built from the text you gave it. Check each box and arrow against the code, and remove or mark anything you cannot confirm.

Where should the diagrams live?

Keep them as text in the repository, for example in docs/architecture.md. Then they change with the code, and reviewers can see the difference line by line. Stamp them with the commit and date you checked them.

Written by
PromptFable Editorial
Last updated 19 Nov 2026
Review
Reviewed by Waseem Nadir, Technical Reviewer
Last reviewed 27 Nov 2026

References

Primary sources for the technical details in this lesson. Vendor platforms change — check the official page if anything here looks different from what you see.

PromptFable is independent and is not affiliated with, endorsed by or sponsored by Anthropic, Google, OpenAI or any other vendor. Claude, Gemini, Google AI Studio and other product names are trademarks of their respective owners.