OpenAI Agents SDK · Chapter 4 of 24
Function tools
By the end of this chapter you can turn a Python function into a tool with @function_tool, say what the SDK builds from it (a name, a description, a strict parameter schema), give an agent tools, read the two requests behind a tool call (a function_call out, a function_call_output back in), go through a run's items, explain why a tool must return text, and say who really runs a tool. 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 1 the agent said hostel rooms cost ₹8,000 with meals extra, because it had never seen Shikhar's policy. Typed output (chapter 3) made its answers tidy, not true. Nidhi's fee register, the timetable, the test results and the refund rules all live in Shikhar's own systems, which your lab has as import shikhar: plain Python functions like shikhar.fee_status("S2041") that return dictionaries.
A model cannot call Python. But it can ask your program to call a function, by name, with arguments, and read what comes back. That is a tool, and it is how an agent stops guessing.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
fee_tool.py: a fee_status tool
Write a tool named fee_status with the SDK's function_tool decorator. It takes a student_id (a str, with its type hint), has a docstring that says what it returns, and returns json.dumps(shikhar.fee_status(student_id)).
Give it to an agent for lab-chat with the instructions You are Shikhar Classes' helpdesk assistant. and run it on the parent's message exactly:
S2041 ki kist kitni late hai? fine lagega?
Print the final output, then a second line: Requests: and the number of requests the run made, from its usage.
- Task 2
two_tools.py: two tools, the model picks
Write two tools, each taking a student_id (a str) and each with a docstring that says what it returns: refund_quote, returning json.dumps(shikhar.refund_quote(student_id)), and test_results, returning json.dumps(shikhar.test_results(student_id)).
Give both to one agent for lab-chat, with the instructions You are Shikhar Classes' helpdesk assistant., and run it on these two messages, in this order, printing each run's final output on its own line:
beta ghar wapas aa raha hai, fees refund kitna milega? S2053
S2033 ke last test ka result kya aaya?
- Task 3
new_items.py: read the run's items
Write a tool upcoming_batches that takes a course (a str) and returns json.dumps(shikhar.upcoming_batches(course)). Give it to an agent for lab-chat with no instructions and run it on:
NEET ka naya batch kab start hoga? seats bachi hai?
Then go through the result's new_items and print one line per item: the item's type, a space, then for a tool_call_item the tool's name, a space and its arguments (both from the item's raw_item); for a tool_call_output_item the tool's output; for a message_output_item the message's text, from ItemHelpers.text_message_output.
- Task 4 · interview question
Interview: never_runs.py, who runs the tool?
Write a tool scholarship_for that takes a score (an int) and returns json.dumps(shikhar.scholarship_for(score)). Inside it, before returning, print: Tool ran: scholarship_for(score), with the score it was given (for example: Tool ran: scholarship_for(76)).
Give it to an agent for lab-chat with the instructions You are Shikhar Classes' helpdesk assistant., run it on My daughter scored 84 in SSAT, how much scholarship will she get? and print the final output after the run.
The interviewer will ask who runs a tool, the model or your program, and how you know.
What you will learn
- A tool is a function with type hints and a docstring.
- @function_tool and the schema it builds.
- The two requests behind a tool call, read from lab requests.
- The run's items: tool calls, tool outputs, messages.
- Tools return text, and why a dictionary goes wrong.
More in Your first agents
Build "Function tools" 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.