AI ordering agent project · Chapter 6 of 12

Conversations in Postgres

By the end of this chapter your agent remembers a conversation: every message the model was sent and every message it wrote is stored in Postgres, in order, so Tara can answer the agent's question in her next message, or come back tomorrow. Only she can read her conversation, and two requests saving into it at the same moment can never mix it up. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Remembering
  • 3 tasks
  • Python
  • In the all labs plan

The first 2 chapters of AI ordering agent project are free. This one comes with the all labs plan.

Why you need this

Members do not write everything in one message. On WhatsApp, Tara writes "Book Jain Thali for Tuesday lunch at 12:30 in Thane", Bhavna's team asks "How many?", and Tara answers "2". Many of the 312 pilot orders came in two or three messages like that.

Your agent asks the right question. Then it forgets. Chapter 5's server, two requests from Tara:

{"status":"answered","answer":"Happy to help. Could you tell me how many dabbas you want?","steps":1,"calls":[]}
[200]
{"status":"answered","answer":"Happy to help. Could you tell me which zone to deliver to (Andheri West, Goregaon, Malad, Dadar, Bandra or Thane) and which day you want it?","steps":1,"calls":[]}
[200]

lab requests 1 shows why. The second request carried only this:

system    You are DabbaDrop's ordering assistant in Mumbai. ...
          Today is Sunday 2026-11-01. Delivery slots start at 12:00, 12:30 and 13:00. Prices are whole rupees.
user      2 dabbas please
reply     Happy to help. Could you tell me which zone to deliver to (Andheri West, Goregaon, Malad, Dadar, Bandra or Thane) and which day you want it?
tokens: 616 in, 40 out

As chapter 1 showed, the model keeps nothing between requests. A conversation exists only because you send all of it, every time. So it has to live somewhere between Tara's two requests, somewhere that survives a restart and is shared by every server: Postgres.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    store.py: start a conversation, find the newest

    In agent/store.py, write start_conversation(db, user_id): add a Conversation for the member, commit, return its id. Then latest_conversation(db, user_id): the id of the member's conversation with the highest id, or None when they have none.

    The Check asks for Tara's newest before she has any (None), starts two for her and one for Advait, and wants two committed rows for Tara, their ids returned, and her second one as the newest.

  2. Task 2

    store.py: load a conversation, only the member's own

    In agent/store.py, write load_messages(db, conversation_id, user_id): the message column of each of the conversation's rows in agent_messages, ordered by position, as a list of dicts. When the conversation does not exist or is not this member's, raise not_found("There is no conversation <id>"): the same 404 both times.

    The Check loads a new conversation (empty), stores two messages at positions 1 and 0 with SQL and wants them back in position order, then wants a 404 AppError for Advait's conversation loaded as Tara and for conversation 999.

  3. Task 3 · interview question

    Interview: store.py, save the new messages

    In agent/store.py, write save_messages(db, conversation_id, messages): append the messages at the next positions (the highest position so far plus 1, or 0 for an empty conversation), set the conversation's updated_at to func.now(), and commit once, all in one transaction; on any error roll back and raise it again. Return how many it added.

    The Check saves two messages (positions 0 and 1, updated_at now), then a tool call (position 2, stored as it was). Then another transaction of the Check's own takes position 4 while your save of two messages runs and commits first: your save must raise Postgres's IntegrityError and keep neither message. Then, as Tara through the app, the agent asks how many dabbas, she answers "2 dabbas please" in the next request, and the run must go on from the stored conversation (find_slots, find_dish, place_order), with every message stored.

    Kunal will ask: why store every message, the tool calls and the tool results too, and not just what Tara typed and what the agent answered?

What you will learn

  • Memory is the message list: what the model "remembers" is what you resend.
  • Two tables: conversations, and messages stored as JSONB at a position.
  • A row and its id: add, commit, read the id; one value with db.scalar, or None.
  • Only the member's own: the same 404 for "not yours" and "does not exist".
  • The next position: max() in a select, and one transaction for the whole save.
  • Two saves at once: how the unique (conversation_id, position) refuses the second, as an IntegrityError.
  • What it costs: every turn resends everything, so prompts grow.

More in Remembering

Build "Conversations in Postgres" yourself

DabbaDrop members want to order by chat. You build the agent that books their tiffins through DabbaDrop's own API, and asks before it pays. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of AI ordering agent project are free. This one comes with the all labs plan.