> ## Documentation Index
> Fetch the complete documentation index at: https://docs.celesto.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Serve agents as an API

> Serve Agentor agents as production REST APIs with serve(), then host the resulting ASGI app on your own infrastructure with Docker or Kubernetes.

You can connect an agent to other applications and services by exposing an API endpoint. Agentor makes it easy to create a production-ready server.

`agent.serve()` returns an ordinary ASGI app running under uvicorn, so you host it the way you host any other Python service. It adds no authentication of its own.

## Serve an Agent as API

Agents can be deployed as REST API server so you can query them from your applications or integrate them into your existing infrastructure.

Agentor makes it easy to serve the Agents by providing a simple `serve` method.

```python theme={null}
from agentor.tools import GetWeatherTool
from agentor import Agentor

agent = Agentor(name="Weather Agent", model="gpt-5-mini", tools=[GetWeatherTool()])
agent.serve(port=8000)   # [!code ++]
```

To query your Agent server:

<CodeGroup>
  ```python Local Deployment theme={null}
  import requests

  URL = "http://localhost:8000/chat"

  response = requests.post(
      URL,
      json={"input": "how are you?"},
      headers={"Content-Type": "application/json"}
  )
  print(response.content)
  ```

  ```python Remote Server theme={null}
  import requests

  # Wherever you are hosting the agent. `serve()` adds no authentication of its
  # own, so put it behind whatever your infrastructure already uses.
  URL = "https://agents.example.com/chat"

  response = requests.post(
      URL,
      json={"input": "how are you?"},
      headers={"Content-Type": "application/json"},
      timeout=(5, 120),
  )
  print(response.content)
  ```

  ```bash cURL theme={null}
  curl -X 'POST' \
    'http://localhost:8000/chat' \
    -H 'accept: application/json' \
    -H 'Content-Type: application/json' \
    -d '{
    "input": "What is the weather in London?"
  }'
  ```
</CodeGroup>

## Self-Hosted Deployment

Deploy agents on your own infrastructure using Docker or Kubernetes.

<Tabs>
  <Tab title="Docker">
    Create a `Dockerfile` in your project:

    ```dockerfile theme={null}
    FROM python:3.11-slim

    WORKDIR /app

    COPY requirements.txt .
    RUN pip install --no-cache-dir -r requirements.txt

    COPY . .

    EXPOSE 8000

    CMD ["python", "agent.py"]
    ```

    Build and run your container:

    ```bash theme={null}
    docker build -t my-agent .
    docker run -p 8000:8000 my-agent
    ```
  </Tab>

  <Tab title="Kubernetes">
    Create a deployment manifest:

    ```yaml theme={null}
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: agent-deployment
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: my-agent
      template:
        metadata:
          labels:
            app: my-agent
        spec:
          containers:
          - name: agent
            image: my-agent:latest
            ports:
            - containerPort: 8000
            env:
            - name: OPENAI_API_KEY
              valueFrom:
                secretKeyRef:
                  name: agent-secrets
                  key: openai-api-key
    ```

    Deploy to your cluster:

    ```bash theme={null}
    kubectl apply -f deployment.yaml
    ```
  </Tab>
</Tabs>

***

## Environment Variables

Set required environment variables for your deployment:

<ParamField path="OPENAI_API_KEY" type="string" required>
  Your LLM provider API key (OpenAI, Anthropic, etc.)
</ParamField>

<ParamField path="CELESTO_API_KEY" type="string">
  Your Celesto API key for accessing managed tools and services
</ParamField>

<ParamField path="PORT" default="8000" type="integer">
  Port to run the agent server on
</ParamField>

<ParamField path="LOG_LEVEL" default="INFO" type="string">
  Logging level: DEBUG, INFO, WARNING, ERROR
</ParamField>

***

## Monitoring & Logs

<CardGroup cols={2}>
  <Card title="Celesto Dashboard" icon="chart-line" href="https://celesto.ai">
    Monitor agent performance, view logs, and track usage metrics in real-time.
  </Card>

  <Card title="Tracing" icon="chart-line" href="/agentor/tracing">
    Enable tracing with `CELESTO_API_KEY` and inspect runs end-to-end.
  </Card>

  <Card title="Health Checks" icon="heart-pulse">
    Built-in health endpoint at `/health` for monitoring and load balancers.
  </Card>
</CardGroup>

<Info>
  For production deployments, we recommend setting up monitoring, logging, and auto-scaling based on your traffic patterns.
</Info>
