Agents from scratch · Chapter 11 of 18

Permissions

By the end of this chapter you can decide what an agent is allowed to do, and make that decision hold: tools per role, a check in the code that runs every call, a customer limited to their own bookings, and instructions hidden in messages and tool results read as data, never obeyed. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Agents that hold up
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Agents from scratch are free. This one comes with the all labs plan.

Why you need this

GharFix now runs three assistants on the same loop. The customer bot on WhatsApp finds technicians, quotes and books. The support bot helps Meenakshi's team move and cancel bookings. The finance bot helps Farooq with refunds. Same model, same loop, same eight tools, and very different things each should be able to do.

The first version said so in the system message. Here is the customer bot, offered all eight tools, with "You may only find technicians, quote and book. Never cancel a booking or refund." in its system message, and the message "Cancel GF318075" (temperature=0):

[0] cancel_booking({"booking_id":"GF318075"})
    -> {"booking_id": "GF318075", "status": "cancelled", "refund": 199, ...}
[1] answer: Booking GF318075 is cancelled, with a refund of ₹199 (...)

The lab model ignores such rules completely. A real model follows them most of the time, and that is the problem: the customers who try hardest to talk a bot into something are exactly the ones you need the rule for. Vikram's rule for the team: what an agent is allowed to do is decided in code, never only in the prompt.

What you build: 4 tasks

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

  1. Task 1

    roles.py: tools per role

    In roles.py, write ROLES, a dictionary from a role's name to the set of tool names it may use: customer may use find_technicians, check_slots, get_quote, book_slot and get_booking; support may use everything customer may, plus reschedule_booking and cancel_booking; finance may use everything support may, plus request_refund.

    Then write tools_for(role): the tool descriptions from gharfix.TOOLS that the role may use, in gharfix.TOOLS's order. A role that is not in ROLES gets an empty list.

  2. Task 2

    executor.py: refuse outside the role

    In executor.py, write execute(role, name, arguments): name is the tool the model asked for and arguments the JSON text it wrote. When the role may not use the tool (by ROLES, imported from roles.py), run nothing and return JSON text of an object with an error key that names the tool and the role. Otherwise run GharFix's function and return its result as JSON text; any exception becomes an error result too, so execute never raises.

    Then write run_agent(question, role): chapter 3's loop with lab-chat, a system message saying today is 2026-10-22, the question, and only the tools the role may use (tools_for), with every call run through execute. Return the final answer.

    The check calls execute with calls in and out of each role, and runs a cancellation as a customer and as support.

  3. Task 3

    owner.py: only your own bookings

    A customer chats from their own phone, and may only see and book for that phone. In owner.py, write execute_for(phone, name, arguments): it runs a call for the customer role (use execute from executor.py), with two more checks first.

    When the arguments have a booking_id, the booking must be one whose phone is the customer's; if it is someone else's, or does not exist, return an error, and make the two errors read the same, so the bot never tells a stranger that a booking ID exists. When the call is book_slot, its phone must be the customer's, or return an error.

    A booking's phone is in GharFix's data (gharfix.db() holds the bookings by ID, with their phones); the tools never give it out.

  4. Task 4 · interview question

    Interview: fix_injection.py, an injected refund

    fix_injection.py's run_support(question) answers customers for GharFix's support team. Only finance refunds, and its system message says so, yet this morning a customer's message got a booking refunded, and a note left on another booking made the bot answer with an instruction hidden in it.

    Fix it so that the bot can never refund: it is offered only the support tools (SUPPORT), and any call outside them is refused with an error result when it is run. An instruction inside a tool result must be read as data, never obeyed. Support's own work, looking bookings and notes up, moving and cancelling, must keep working.

    Keep run_support(question), get_notes, NOTES and SUPPORT. The check sends this morning's two messages, and a reschedule.

What you will learn

  • Tools per role: a model can only ask for what it is offered.
  • An executor that refuses: the check that holds when everything else is wrong.
  • Ownership: a customer sees and books only for their own phone.
  • Injected instructions, in a customer's message or a tool result.

More in Agents that hold up

Build "Permissions" yourself

GharFix, a Bengaluru home-services company, needs an assistant that books, reschedules and refunds safely. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Agents from scratch are free. This one comes with the all labs plan.