Lesson 2 of 5 Building Agents and Tool Integrations

Implementing Tool Use with the Converse API

This lesson explains the tool use flow in the Converse API, also called function calling, which lets a model request that the application run a function with structured arguments.

Learning outcomes

After completing this lesson, you will be able to:

  • Define tools with names, descriptions and input schemas.
  • Handle the tool use request and response cycle.
  • Handle parallel tool calls and streaming.

Defining tools

With the Converse API, tools are passed in the toolConfig parameter. Each tool has a name, a description that tells the model what the tool does and when to use it, and an input schema in JSON Schema that defines its arguments, their types and allowed values, and which arguments are required. The model never runs a tool itself: when it decides a tool is needed, it returns a tool use request with the arguments, the application runs the tool and sends the result back in the next message.

The toolChoice setting controls whether the model may answer without calling a tool (auto), must call one of the tools (any), or must call a specific named tool (tool). Forcing a specific tool is a common way to obtain structured output, because the tool's input schema then defines the shape of the response.

The following diagram compares the two tool definitions.

Why the model never called the tool. A tool named get_data and described as gets data gives the model no reason to use it for shipment questions. Renaming it get_shipment_status, describing when to use it and defining an order_id parameter with its format fixes tool selection.
The model chooses tools only from their names, descriptions and schemas.
Fix the problem

Tools the model misuses

Exam skills 2.1.6 · 5.2.3

Find the fix for each tool definition problem.

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

The request and response cycle

When the model decides to use a tool, the response has stopReason tool_use and the assistant message contains one or more toolUse blocks, each with a toolUseId, the tool name and the input arguments. The application runs the tool, then sends a new request that includes the assistant message and a user message containing a toolResult block with the same toolUseId and the result content. If the tool failed, the result has status error and an explanation, so the model can recover. The loop repeats until stopReason is end_turn.

messages = [{"role": "user", "content": [{"text": "What is the status of order 1182?"}]}]
while True:
    r = brt.converse(modelId=model_id, messages=messages, toolConfig=tool_config)
    msg = r["output"]["message"]
    messages.append(msg)
    if r["stopReason"] != "tool_use":
        break
    results = []
    for block in msg["content"]:
        if "toolUse" in block:
            use = block["toolUse"]
            try:
                out = TOOLS[use["name"]](**use["input"])
                results.append({"toolResult": {"toolUseId": use["toolUseId"], "content": [{"json": out}]}})
            except Exception as e:
                results.append({"toolResult": {"toolUseId": use["toolUseId"], "status": "error",
                                               "content": [{"text": f"Tool failed: {e}"}]}})
    messages.append({"role": "user", "content": results})

The following diagram shows one tool call passing between the application, the model and the tool.

The tool use cycle with the Converse API. The application sends messages with toolConfig; the model replies with stopReason tool_use and a toolUse block; the application runs the tool and returns a toolResult with the same toolUseId; the model then gives the final answer with stopReason end_turn.
The model never runs the tool itself.
Put in order

The tool use cycle

Exam skills 2.1.6 · 2.4.1

Put the steps of one tool call through the Converse API in order.

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

Parallel tool calls and streaming

Some models request several tools in one turn, for example looking up two orders at once; the application can run them in parallel and return all results in one user message. With ConverseStream, tool use arrives as streamed events, and the application assembles the tool input from content block deltas before running the tool. A production loop also needs an iteration limit, timeouts on tool execution and logging of every call. The following table summarises the key fields.

FieldMeaning
toolConfig.tools[].toolSpecName, description and JSON Schema of each tool
toolChoice auto / any / toolModel may, must, or must use a named tool
stopReason tool_useThe model is waiting for tool results
toolUse blocktoolUseId, name and input arguments
toolResult blockResult for a toolUseId; status error on failure
Worked example

The model never calls the tool

An assistant has a tool get_data described as "Gets data". Users ask about shipment status, and the model answers from general knowledge instead of calling the tool.

The description gives the model no reason to use it. Renaming it get_shipment_status, describing it as "Returns the live status, location and estimated delivery date of a shipment. Use for any question about where an order is", and defining an order_id parameter with its format fixes tool selection.

Check yourself

A tool call fails. How should the failure be returned to the model?

Show answer

As a toolResult with the same toolUseId and status error, so the model can recover or try another approach.

Exam Tips

Remember for the exam

  • Tools need a clear name, a description of when to use them and a JSON Schema.
  • stopReason tool_use: run the tool and return toolResult with the same toolUseId.
  • Return tool errors with status error so the model can recover.
  • toolChoice forces a specific tool for structured output.