Start learning free

LESSON 90 OF 129 · AI ENGINEERING: AGENTS

When Not to Use an Agent in Python

Practice

An agent lets the model choose every step. That costs one model call per step: seven calls for one return. And the model decided nothing surprising on the way, because the steps of a return are the same every time.

When you know the steps before the run starts, write them yourself. That is a workflow: your code decides the steps, and the model does only what code can't do. The tool functions are plain Python functions, so your code can call them without asking a model:

stock = check_stock({"product_id": 3})
if stock["stock"] == 0:
    print("Sold out")

Here an if decides, not a model: it costs nothing and it decides the same way on every run. In this lesson's workflow the model has one job left, writing a friendly reply, in a call without tools and without a loop.

The finished program runs the same return both ways. After the agent's trace it prints:

Workflow:
  Customer is asked: Start the return of order 8?
  Customer answers: yes
  Result: {'id': 8, 'customer': 'Ana Pop', 'ordered': '2026-05-28', 'status': 'returning'}
  Reply: Hi Ana, good news: the return of order 8 has started. We'll write to you when your parcel reaches us.
Agent: calls 7 cost 0.0092
Workflow: calls 1 cost 0.0004

Your task: finish run_workflow: find the order with the product in two loops, call start_return, and ask the model once for the reply. Then make both functions return their calls and cost, and print the comparison, with each cost rounded to 4 decimals. start_return calls customer_confirms(question). This helper already exists behind the lesson, so don't write your own. It prints the "Customer is asked" and "Customer answers" lines and returns True when the customer says yes.


Assignment
Follow the Coding Tutorial to write the return as a fixed workflow and to compare it with the agent.


Hint
Each tool function takes one dictionary and returns one, so the list you need is under a key of the result: ["orders"] or ["items"]. The workflow's model call has no tools argument.


STEP 1 OF 12
    Ctrl + Enter to check

    Ai When Not Agent in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free