Build safe tools and compare deterministic workflows with agentic decision-making.

Tools and agent workflow labs

These labs cover exercises 7 and 8. Start with read-only tools and simulated actions.

Prerequisites and setup

Know Python functions, dictionaries, exceptions, and file handling. Create a virtual environment and begin with tools that only calculate, search, or read local fixtures. Define each tool’s input and output schema before connecting it to a model. Use a step limit, timeout, audit log, and explicit confirmation for any side effect.

7. Tool-use assistant

Implement a local assistant with a calculator, unit converter, glossary search, and CSV statistics tool. Each tool must have a schema, input validation, a timeout, and an audit record.

def calculate(expression: str) -> float:
    allowed = set("0123456789+-*/(). ")
    if not expression or set(expression) - allowed:
        raise ValueError("Only arithmetic characters are allowed.")
    return eval(expression, {"__builtins__": {}}, {})

TOOLS = {
    "calculate": calculate,
}

Do not use unrestricted eval in a real application; this restricted classroom example exists to demonstrate why validation and a safer parser are required.

Record:

trace = {
    "tool": "calculate",
    "arguments": {"expression": "12 * 4"},
    "validated": True,
    "result": 48,
    "approved_by_user": True,
}

Test invalid arguments, unavailable tools, tool errors, repeated calls, and a request that needs human confirmation.

8. Fixed workflow versus agent workflow

Implement one task two ways.

Fixed workflow:

document = load_document(path)
facts = extract_facts(document)
validated = validate_facts(facts)
summary = write_summary(validated)

Agent workflow:

while not finished:
    decision = choose_next_action(state, available_tools)
    result = execute_read_only_tool(decision)
    state = update_state(state, result)

Compare completion rate, steps, latency, cost, reproducibility, debugging difficulty, and failure severity. Prefer the fixed workflow when the task is known and stable; use agentic behavior only when the flexibility is worth the added uncertainty.

Completion checklist

  • Tools are read-only or simulated.
  • Arguments are validated before execution.
  • Tool calls and failures are logged.
  • The agent has a step limit and timeout.
  • Side effects require explicit confirmation.
  • Fixed and agent versions use the same test set.

References