Lesson 2 of 2 Building Agent Client Applications

Handling Agent Responses and Completing Function Calls

This lesson explains how a client reads an agent's response, which records the tool calls made during the run, and how it completes function calls, for which the agent asks the client to do the work.

Learning outcomes

After completing this lesson, you will be able to:

  • Interpret the output items in an agent response.
  • Explain how a client completes a function call and returns its result.
  • Identify the errors and safety measures a client must handle.

Output items

The response's output_text property gives the final text. The output property is a list of items produced during the run, each with a type, such as message, file_search_call, code_interpreter_call or function_call. Inspecting the item types shows which tools the agent used, and message content can include annotations such as file citations from file search.

Match the pairs

Reading the output items

Exam skills 2.1.5

Match each output item type with what it tells the client.

Log in to try this activity Interactive activities are free with an account.

Completing function calls

Built-in tools such as file search and code interpreter run inside Foundry. A function tool is different: the function's code lives in the client application. When the agent decides to call it, the response contains a function_call item with the function name, a call_id and JSON arguments. The client runs the function and sends the result back as a function_call_output item with the same call_id on the same conversation; the agent then completes its answer.

import json
response = agent_client.responses.create(conversation=conversation.id, input=question)
calls = [item for item in response.output if item.type == "function_call"]
if calls:
    outputs = [{"type": "function_call_output", "call_id": c.call_id,
                "output": json.dumps(get_order_status(**json.loads(c.arguments)))} for c in calls]
    response = agent_client.responses.create(conversation=conversation.id, input=outputs)
print(response.output_text)

The following diagram shows the messages exchanged for one function call.

Completing a function call. The client sends the user's question to the agent. When the agent decides to use a function tool, the response contains a function_call item with the function name, a call_id and JSON arguments, and no final text. The client runs its own function and sends the result back as a function_call_output item with the same call_id on the same conversation. The agent then completes its answer.
The call_id links the function's output to the agent's request.
Spot the bug

A function result that goes nowhere

Exam skills 2.1.5

Find the lines that stop the agent from receiving the function result.

Log in to try this activity Interactive activities are free with an account.

Errors and safety

Clients should handle the same errors as any model client: throttling (HTTP 429), content blocked by the agent's guardrail, and network failures. Because function tools run with the application's permissions, the function code should validate its arguments, for example checking that an order number belongs to the signed-in customer, rather than trusting the model.

Following a function call step by step

A function tool call involves two round trips between the client and the agent. Consider an agent with a get_order_status function. The steps are as follows.

  1. Send the message

    The client sends the user's message, Has order 1042 shipped?

  2. Receive a function call

    The agent decides it needs the function and returns a function_call item with the name get_order_status, a call_id and the arguments {"order_id": "1042"}.

  3. Run the function

    The client checks that the order belongs to the signed-in customer, runs its own function and gets the result, for example {"status": "shipped"}.

  4. Return the output

    The client sends the result back as a function_call_output item with the same call_id.

  5. Read the answer

    The agent uses the result to write its final answer, which the client reads from output_text.

If the client does not send the function result, the agent cannot finish its answer. This is the main difference from built-in tools, which complete within a single request.

Worked example

An agent that never finishes

An agent has a get_weather function tool. When users ask about the weather, the client prints an empty answer, and the response output contains a function_call item.

The agent is waiting for the function's result. The client must run get_weather with the supplied arguments and send a function_call_output with the matching call_id, after which the agent returns its answer. Adding the weather data to the agent's instructions would make it stale, and switching to code interpreter would not give it live weather.

Check yourself

Why must a client validate the arguments of a function call?

Show answer

The arguments come from the model, which can be wrong or manipulated, so code must check them before running the function.

Exam Tips

Remember for the exam

  • output_text is the answer; output lists the items, including tool calls.
  • Function calls must be executed by the client and returned as function_call_output with the same call_id.
  • Validate function arguments in code; do not trust the model's input.