Deploying Your AI Agent › Running Your Agent as a Service

Michael BrenndoerferAugust 18, 20252 min read
AI Agent HandbookAll chapters
Part 14 of 15Deployment and Operations - From Prototype to Production
Chapter 39Deploying Your AI Agent › Running Your Agent as a Service
Reading section 5 of 10 · Running Your Agent as a Service · 2 minSection 5 of 10 · 2 min

Book contents

Section 5 of 10 · 2 min read

Deploying Your AI Agent › Running Your Agent as a ServiceOne section · Sections in this chapter5 / 10 ↓

Once your code is packaged, you need to decide how it will run. For a development script, you manually start it and interact through the terminal. For a deployed agent, you want it to run continuously and accept requests programmatically.

Creating a Simple API

The most common approach is to wrap your agent in a web API. This lets other programs (including web interfaces, mobile apps, or other services) interact with your agent by sending HTTP requests.

Here's a minimal example using Flask, a lightweight Python web framework:

In[2]:
Code
## Using Claude Sonnet 4.5 for its superior agent capabilities
from flask import Flask, request, jsonify
from anthropic import Anthropic
import os

app = Flask(__name__)
client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))


@app.route("/chat", methods=["POST"])
def chat():
    user_message = request.json.get("message")

    # Call your agent logic here
    response = client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=1024,
        messages=[{"role": "user", "content": user_message}],
    )

    return jsonify({"response": response.content[0].text})


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

Now your agent listens on port 5000. You can send it a message like this:

In[4]:
Code
curl -X POST http://localhost:5000/chat \
  -H "Content-Type: application/json" \
  -d '{"message": "What is the weather like?"}'

This simple API can be extended to handle conversation history, tool calls, and all the other features you've built. The key insight is that you're transforming your agent from an interactive script into a service that responds to requests.

Running in the Background

On a local machine or server, you want your agent to start automatically and keep running even if you log out. On Linux or macOS, you can use systemd or launchd to create a service. On Windows, you can use Task Scheduler or run it as a Windows service.

For a simple approach, you might use a process manager like pm2 (originally for Node.js but works with Python too) or supervisor. These tools restart your agent if it crashes and provide logging.

Example with pm2:

In[6]:
Code
pm2 start assistant.py --name my-assistant
pm2 save
pm2 startup

Now your assistant runs in the background and starts automatically on boot.