Coding with AI

Transforming a Python Script into an AI Agent: A Guide

A step-by-step tutorial showing developers how to convert an existing Python script—such as a website monitor—into an AI agent. By adding the OpenAI Agents SDK and a simple @function_tool decorator, existing functions become tools the LLM can call autonomously. The agent decides when to invoke functions, interprets results, and answers natural-language requests, replacing rigid hardcoded logic with flexible agentic workflows.

Transforming a Python Script into an AI Agent: A Guide

If you already write Python scripts that get useful work done, you can start experimenting with AI agents today—no rewrite required. This tutorial is for developers who want to add natural-language control to their existing automation. We'll take a simple website-monitoring script, expose its function as a tool, and let an LLM decide when and how to use it with the OpenAI Agents SDK.

Transforming a Python Script into an AI Agent: A Guide

Featured image: abstract, friendly illustration of a Python script transforming into a connected network of glowing nodes.

Starting With a Normal Python Script

Before adding any AI, let's look at an ordinary Python program. Imagine we want to check whether a website is up and see how long the request takes. The script sends an HTTP request, records the status code, measures the response time, and returns the results.

The script does exactly what we told it to do—and nothing more. That's the limitation. The workflow is completely fixed. If we want to check five websites, compare their response times, or figure out which one looks unhealthy, we have to write all that extra logic ourselves.

This is where an AI agent changes things. Rather than encoding every possible decision in Python, we can expose `check_website()` as a tool and hand the model a goal. The model then decides when to call the function, which URLs to check, how many times to run it, and what to do with the outputs.

Step 1: Installing the Agents SDK

Set up a Python project and install the packages needed to build and run the agent. Create a new project, or simply use pip to add the OpenAI Agents SDK to your environment.

You'll also need to set your OpenAI API key as an environment variable.

Transforming a Python Script into an AI Agent: A Guide

The Agents SDK provides a lightweight runtime that handles agents, tools, handoffs, sessions, and tracing, so you can focus on your own code.

Step 2: Turning the Python Function Into a Tool

Next, we expose the existing function so the model can choose to call it. Almost everything stays the same—the main change is adding the `@function_tool` decorator on top of the function.

The OpenAI Agents SDK automatically converts the function signature into the JSON schema the model requires. It also uses the function name and docstring to describe what the tool does. That means no manual schema writing: your docstring becomes the tool's description.

Step 3: Creating the Agent

Now define an `Agent`, describe what it should do, and give it access to the `check_website()` tool. Then run it with the SDK's `Runner`.

With the old approach, we would have needed to write the looping and comparison logic by hand for every new request. Instead, the model interprets the request, calls `check_website()` for the three websites, receives the results, compares them, and produces a natural-language answer.

How the Agent Loop Works

Behind the scenes, the `Runner` manages the back-and-forth between the model and the tools. Conceptually, the loop looks like this: the model receives a request, decides which tool to call, gets the result, and evaluates whether it has enough information. If not, it calls the tool again—perhaps with different arguments. The loop continues until the model can produce a final response.

This is what makes the workflow agentic. Instead of following a fixed sequence written entirely in Python, the model chooses its actions based on the request and the results it receives along the way.

Transforming a Python Script into an AI Agent: A Guide

Other Python Scripts You Can Turn Into Agents

The same pattern applies to almost any existing Python automation. Keep the Python functions that do the actual work, and let the agent decide which functions to call and how to combine their results. A few examples:

  • CSV analyzer: Functions filter rows, compute metrics, and find trends; the agent answers natural-language questions about the data.
  • Server monitor: Functions check CPU, memory, disk, and processes; the agent investigates why a server looks unhealthy.
  • Log analyzer: Functions search logs, count errors, and extract events; the agent investigates incidents and summarizes what happened.
  • API automation: Functions fetch data, update records, or create reports; the agent decides which operations are needed and in what order.

With the OpenAI Agents SDK, you can expose any of these functions with `@function_tool` and add them to the agent's tools list. The Python code still performs the real work; the agent adds natural-language understanding, tool selection, and orchestration on top.

Common Mistakes

  • Skipping the docstring. The SDK uses your function's docstring to describe the tool to the model, so a vague or missing docstring leads to poor tool usage.
  • Encoding all the logic in the prompt instead of tools. Let focused Python functions do the work; keep the agent's job to deciding and orchestrating.
  • Forgetting the API key. The agent won't run without your OpenAI API key set in the environment.
  • Expecting fixed behavior. The model decides how many times to call a tool and with what arguments—if you need strict, repeatable behavior, a plain script is still the better choice.

Final Thoughts

Agentic AI is becoming a practical way to automate workflows, with more companies using agents for multi-step tasks rather than rigid scripts. Meanwhile, cheaper models such as GPT-5.6 Luna make it far more affordable to run tool-using—and even multi-agent—systems at scale.

In this guide, we started with an ordinary Python function, turned it into a tool, connected it to an agent, and let the `Runner` handle the decision-making loop. That's the core idea behind agentic applications: give the model a goal and the right tools, then let it figure out how to finish the job.

Meta description: Learn how to turn any Python function into an AI agent tool using the OpenAI Agents SDK, with a step-by-step website-monitoring example. (147 characters)

Tags: OpenAI Agents SDK, Python tutorial, AI agents, function tools, agentic AI

Comments (0)

  1. No comments yet. Be the first to share what worked for you.

Leave a comment

Comments are reviewed before they appear. Your email address is not published.