Agent Governance Toolkitagent-governance-toolkit 4.1.0 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
23 small wins to finish your pathNext lesson

The agent that decides

Lesson 1 had a decide function with one if in it. This lesson grows it into the agent the rest of the course governs, and puts it in a file you import everywhere.

The toolkit governs tool calls, so the course needs something producing them. A real agent would ask a model. A model costs a key, and the thing being taught here is the gate rather than the model, so instead the agent reads the message itself.

It has to genuinely decide, or every later lesson is watching a script replay. So the rules look at words and pull numbers out of the sentence.

Four tools

Example
ORDERS = {
    "A17": {"item": "Noise-cancelling headphones", "paid": 249.0, "status": "delivered"},
    "B22": {"item": "Laptop stand", "paid": 39.0, "status": "in transit"},
}
REFUNDS, OUTBOX = [], []

def issue_refund(order_id, amount):
    REFUNDS.append({"order_id": order_id, "amount": amount})
    return {"refunded": amount, "order_id": order_id}

print(issue_refund("A17", 249.0))

REFUNDS and OUTBOX are lists so that a lesson can look at what really happened rather than trusting what a function claimed to do. The full file has lookup_order and send_email beside this one, written the same way.

The router

The agent is a class with one method. Give it a message and it returns the name of a tool and the arguments for it, which is the same pair a model would produce.

Example
import re

class PretendAgent:
    def decide(self, message):
        text = message.lower()
        order_id = (re.search(r"\b([A-B]\d{2})\b", message) or [None, "A17"])[1]
        amount = re.search(r"(\d+(?:\.\d+)?)", message.replace(order_id, ""))
        if "refund" in text or "money back" in text:
            return "issue_refund", {"order_id": order_id,
                                    "amount": float(amount.group(1)) if amount else 249.0}
        return "lookup_order", {"order_id": order_id}

print(PretendAgent().decide("refund 100 on order A17"))

The order id is pulled out first and removed from the text before the amount is searched for, or A17 would donate its own 17 as the refund.

The file on disk adds two more branches, for send_email and delete_order, and a script argument that makes the agent replay a fixed list of calls instead of deciding. Lesson 17 uses that when a lesson needs exactly four calls in a row.

The file

Save this as pretend_agent_governance.py next to your lessons. Every later lesson imports from it.

Example
from pretend_agent_governance import PretendAgent, TOOLS, ORDERS

agent = PretendAgent()
for message in ["Where is A17?", "I want my money back on B22", "delete A17"]:
    print(agent.decide(message))

Three messages, three different tools, and nothing was asked of a model. That is the agent the rest of the course puts a gate in front of.

Try it yourself
  • Ask for a refund of 40 on order B22 and check both the id and the amount come through.
  • Send a message with no order id in it at all and find the fallback.
  • Add a branch for the word cancel that returns delete_order, then remember in lesson 5 that you did.

You understood something today that you didn't yesterday.