liveBook · Manning

Tool usage

As explained in AI Agents in Action

Function calling is an LLM interface capability that produces a structured request to invoke a named function with extracted arguments.

Tool use lets an LLM match a request to a named function and return structured arguments. The model suggests the call; application code executes the matching function. section 5.2.1

A basic tool-use workflow is:

  • Define a function name, description, and object-shaped parameter schema. section 5.2.1
  • Send the user request and tool specification to the LLM, which selects a function and extracts arguments. section 5.2.1
  • Parse the returned name and arguments, invoke the corresponding function, and send its result back for a final response. section 5.2.1 section 7.5

Recommendation and weather tools

A request for a good time-travel movie produces a recommend call with topic and rating arguments. An agent can also expose get_current_weather; its action decorator generates an OpenAI-compatible tool specification, and both calls may execute in parallel. section 5.2.1 section 7.5

Listing 7.14 oai_agent.py (get_response_stream: execute tool calls)
if tool_calls:     #1
    available_functions = {
        action["name"]: action["pointer"] for action in self.actions
    }     #2
    self.messages.append(
        response_message
    )
    for tool_call in tool_calls:     #3
        function_name = tool_call.function.name
        function_to_call = available_functions[function_name]
        function_args = json.loads(tool_call.function.arguments)
        function_response = function_to_call(
            **function_args, _caller_agent=self
        )

        self.messages.append(
            {
                "tool_call_id": tool_call.id,
                "role": "tool",
                "name": function_name,
                "content": str(function_response),
            }
        )
    second_response = self.client.chat.completions.create(
        model=self.model,
        messages=self.messages,
    )      #4
    response_message = second_response.choices[0].message

Trace how returned tool calls are mapped to available functions, executed, recorded as tool messages, and followed by a second API response. section 7.5

How AI Agents in Action covers it

How deep it goes
Explains schemas, selection, argument extraction, execution, and parallel calls, while deferring ordered action sequences.
Examples
Uses recommendation and weather functions, JSON argument objects, generated tool specifications, and an agent execution loop.
What it assumes
Assumes Python, VS Code, an environment file, API credentials, and basic familiarity with defining functions.
What you can do after
Apply it via an LLM API by defining tools, interpreting structured calls, executing functions, and returning results.
How it connects
Links function calling with LLM APIs, agents, tool specifications, semantic functions, and parallel function calling.

Also explained in

Compare how 13 books explain tool usage →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents in Action. The section links point to the parts of the book it draws on.