CrewAICrewAI 1.15 · Python 3.10 to 3.13
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
32 small wins to finish your pathNext lesson

An agent that keeps going

A model can keep asking for tools and never answer. max_iter caps the loop: after that many rounds, CrewAI takes the tools away and asks for a final answer.

Lesson 9's loop ends when the model answers in words. Nothing forces it to. This model asks for the same lookup every time it has tools, however many results it has already seen.

Examplecrew.py, from lesson 9
from crewai import Agent, Crew, Task
from shop_llm import ShopLLM
from tools import lookup_order

clerk = Agent(
    role="Order clerk",
    goal="Find the status of customers' orders",
    backstory="You can look up any order in the shop's system.",
    llm=ShopLLM(model="shop"),
    tools=[lookup_order],
)
task = Task(description="Answer the customer: {question}",
            expected_output="The order's status in one sentence.", agent=clerk)
crew = Crew(agents=[clerk], tasks=[task])
Examplestuck.py
from shop_llm import ShopLLM


class Stuck(ShopLLM):
    calls: int = 0

    def call(self, messages, tools=None, **kwargs):
        self.calls += 1
        print("model call", self.calls, "with tools" if tools else "without tools")
        if tools:
            return self.decide(messages[:2], ["lookup_order"])
        return "A17 shipped on 3 March. Sorry for the wait."

Stuck passes only the first two messages to decide, so it never sees a tool result and asks again. It prints each call, and answers in words only when it is given no tools.

Example
clerk.llm = Stuck(model="shop")
clerk.max_iter = 3
result = crew.kickoff(inputs={"question": "Where is my order A17?"})
print(result.raw)

Three calls with tools, three lookups. On the fourth, CrewAI sent no tools and a message telling the model to give its best final answer now, and the model did. The Agents page gives the default for max_iter as 20; in 1.15.22 it is 25, which a stuck hosted model reaches slowly and expensively.

Try it yourself
  • Set max_iter to 1 and count the model calls.
  • Print clerk.llm.calls after the run.
  • Remove the if tools branch and see where the run ends.

Little by little, you're building something great.