liveBook · Manning

Agent_action decorator

As explained in AI Agents in Action

The agent_action decorator is a code annotation that marks a function as an action to be discovered and loaded by the Playground.

The agent_action decorator marks a Python function as an agent action. Place the decorated function in the appropriate actions folder, and the Playground discovers and loads it when the function has useful documentation. section 6.2.2

Use the decorator in three practical steps:

  • Write a function with a clear name and documented parameters. section 6.2.2
  • Place the file in the Playground assistant_actions folder. section 6.2.2
  • Add @agent_action so the function can be inspected and represented as an OpenAI-standard tool specification. section 7.5

A file-saving action

@agent_action\ndef save_file(filename, content):\n """Save content to a file."""\n ...\n\nThe Playground can discover this action and make it available to an assistant.

Listing 6.4 playground/assistant_actions/file_actions.py
import os

from playground.actions_manager import agent_action

OUTPUT_FOLDER = "assistant_outputs"


@agent_action     #1
def save_file(filename, content):      #2
    """
    Save content to a file.      #3

    :param filename: The name of the file including extension.
    :param content: The content to save in the file.
    """
    file_path = os.path.join(OUTPUT_FOLDER, filename)
    with open(file_path, "w", encoding="utf-8") as file:
        file.write(content)
    print(f"File '{filename}' saved successfully.")      #4

Inspect the complete file_actions.py listing, including imports, output-path construction, file writing, and the decorator annotation. section 6.2.2

How AI Agents in Action covers it

How deep it goes
Explains folder discovery, documentation requirements, inspection, and tool-schema generation; defers ordered action planning.
Examples
Uses file saving, weather lookup, and recommendation functions as decorated action examples.
What it assumes
Assumes basic Python function writing while teaching action registration and automatic tool exposure.
What you can do after
Build custom agent actions by decorating documented Python functions and placing them in the configured actions folder.
How it connects
Connects custom actions with tools, function calling, OpenAI tool specifications, and parallel function calling.

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.