OpenAI Agents SDK · Chapter 6 of 24

The agent loop

By the end of this chapter you can say what the Runner does turn by turn, read a run's turns from raw_responses, limit a run with max_turns and catch MaxTurnsExceeded, end a run at a tool's result with tool_use_behavior, force or forbid tools with tool_choice (and say why the SDK resets it), and continue a run by hand with to_input_list(). Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Your first agents
  • 4 tasks
  • Python
  • In the all labs plan

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

Why you need this

In chapter 4 one question made two requests: a tool call, then an answer. Some questions need more. "What is S2061's timetable on Monday?" needs the student's batch first, then that batch's classes. A refund question for two brothers needs two quotes. And Bhanu has seen the opposite too: on a bad day a model asks for the same lookup again and again, and every request costs money while the parent waits.

All of this is the Runner's loop. You have used it since chapter 1 without seeing it. This chapter opens it up, and shows the four handles the SDK gives you on it: how many turns, when to stop, which tools the model may use, and how to pick a conversation up again.

What you build: 4 tasks

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

  1. Task 1

    turns.py: count the turns

    Write two tools: get_batch, taking a student_id (a str), with the docstring The batch a student studies in: batch_id and course., returning json.dumps of a dict with the student_id, batch_id and course from shikhar.get_student(student_id); and class_schedule, taking a batch_id and a day (both str), with a docstring, returning json.dumps(shikhar.class_schedule(batch_id, day)).

    Give both to an agent for lab-chat with the instructions You are Shikhar Classes' helpdesk assistant. and run it on: What is S2061's timetable on Monday?

    Then go through the result's raw_responses, one per turn, and print one line per turn: Turn, its number from 1, a colon and a space, then the names of the function calls in that response's output joined with a comma and a space, or answer when it has none. Then print the final output.

  2. Task 2

    stop_at_tool.py: stop at the tool's result

    Write two tools, fee_status and refund_quote, each taking a student_id (a str), with a docstring, returning json.dumps of shikhar's answer. Give both to an agent for lab-chat with the instructions You are Shikhar Classes' fees desk. and a tool use behaviour that stops the run at refund_quote only, made with the SDK's StopAtTools.

    Run it on S2053 ki fees kitni baki hai? and then on S2053 ka refund kitna milega? and print one line per run: the requests from its usage, a colon and a space, then the final output.

  3. Task 3

    continue_run.py: continue a run by hand

    Write a tool refund_quote, taking a student_id (a str), with a docstring, returning json.dumps(shikhar.refund_quote(student_id)). Give it to an agent for lab-chat with the instructions You are Shikhar Classes' refund desk. and run it on: Mujhe refund ke baare me poochna hai, kitna milega?

    Print the final output (the agent asks for the student ID). Then run the agent again on the first result's to_input_list() with one more item added at the end, the user message {"role": "user", "content": "S2053"}, and print that run's final output.

  4. Task 4 · interview question

    Interview: loop_guard.py, a loop that never ends

    Write a tool fee_status, taking a student_id (a str), with a docstring, returning json.dumps(shikhar.fee_status(student_id)). Give it to an agent for lab-chat-loopy, the model that asks for the same tool again after every result, and run it on S2052 ki fees kitni baki hai? with at most 3 turns.

    Catch the SDK's MaxTurnsExceeded by name and print two lines: Stopped: and the exception's message, then Tool calls: and the number of tool call items in the exception's run_data.new_items.

    The interviewer will ask what stops an agent loop that never ends, and what each stop costs.

What you will learn

  • The loop: call the model, run its tools, call it again.
  • Turns in raw_responses, and several calls in one turn.
  • max_turns and MaxTurnsExceeded.
  • tool_use_behavior: stop at a tool's result.
  • tool_choice and reset_tool_choice.
  • to_input_list(): a run as input items, to continue by hand.

More in Your first agents

Build The agent loop yourself

Shikhar Classes, a Kota coaching institute, answers parents and students on WhatsApp all day. You build its helpdesk assistant. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

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