Metadata-Version: 2.4
Name: ants-platform-sdk
Version: 3.8.0
Summary: A client library for accessing ants-platform
License: MIT
Requires-Python: <4.0,>=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: httpx<1.0,>=0.15.4
Requires-Dist: pydantic<3.0,>=1.10.7
Requires-Dist: backoff>=1.10.0
Requires-Dist: wrapt<2.0,>=1.14
Requires-Dist: packaging<26.0,>=23.2
Requires-Dist: requests<3,>=2.33.0
Requires-Dist: opentelemetry-api<2.0,>=1.45.2
Requires-Dist: opentelemetry-sdk<2.0,>=1.45.2
Requires-Dist: opentelemetry-exporter-otlp-proto-http<2.0,>=1.45.2
Requires-Dist: regex>=2023.0.0
Provides-Extra: openai
Requires-Dist: openai>=0.27.8; extra == "openai"
Provides-Extra: langchain
Requires-Dist: langchain>=0.1.0; extra == "langchain"
Dynamic: license-file

<div align="center">
   <img src="https://www.antsplatform.ai/ants-logo.png" alt="ANTS Platform" width="120">
   <h1>ANTS Platform Python SDK</h1>
   <p>Tracing, cost and governance for AI agents.</p>
   <p>
      <a href="https://www.antsplatform.ai/docs"><b>Documentation</b></a>
      &nbsp;·&nbsp;
      <a href="https://www.antsplatform.ai/docs/getting-started/quick-start">Quick start</a>
      &nbsp;·&nbsp;
      <a href="https://www.antsplatform.ai">Website</a>
   </p>
</div>

[![MIT License](https://img.shields.io/badge/License-MIT-red.svg?style=flat-square)](https://opensource.org/licenses/MIT)
[![PyPI Version](https://img.shields.io/pypi/v/ants-platform-sdk.svg?style=flat-square&label=pypi+ants-platform-sdk)](https://pypi.org/project/ants-platform-sdk/)

## Installation

```bash
pip install ants-platform-sdk
```

With the OpenAI integration:

```bash
pip install "ants-platform-sdk[openai]"
```

From 3.7.0 the package is published as `ants-platform-sdk`; the import stays `import ants_platform`. If you used the old `ants-platform` package, remove it first:

```bash
pip uninstall ants-platform && pip install ants-platform-sdk
```

## Quick start

### 1. Set your keys

Create an API key pair in ANTS Platform under **Project Settings > API Keys**, then set:

```bash
export ANTS_PLATFORM_PUBLIC_KEY="pk-ap-..."
export ANTS_PLATFORM_SECRET_KEY="sk-ap-..."
export ANTS_PLATFORM_HOST="https://<your-ants-host>"
```

Always set the host: use the address your administrator gives you.

### 2. Trace an agent and its OpenAI calls

```python
from ants_platform import AntsPlatform
from ants_platform.openai import openai  # traced drop-in for the OpenAI SDK

ants = AntsPlatform()  # reads the ANTS_PLATFORM_* variables


def answer(question: str) -> str:
    with ants.start_as_current_span(
        name="support-request",
        agent_name="support_agent",
        input={"question": question},
    ) as span:
        ants.update_current_trace(user_id="user-123", session_id="session-1")
        response = openai.chat.completions.create(
            model="gpt-4o-mini",
            messages=[{"role": "user", "content": question}],
        )
        text = response.choices[0].message.content
        span.update(output={"answer": text})
        return text


print(answer("How do I reset my password?"))
ants.flush()  # send buffered traces before a short script exits
```

The outermost span needs an `agent_name`; everything inside it is attributed to that agent. The OpenAI call is recorded as a generation with model, tokens, cost and latency.

### 3. Trace your own steps

Decorate functions with `@observe` to record them as steps inside the agent's trace:

```python
from ants_platform import AntsPlatform, observe

ants = AntsPlatform()


@observe()
def search_kb(query: str) -> list[str]:
    return ["Open Settings > Security > Reset password"]


@observe(as_type="tool")
def create_ticket(summary: str) -> str:
    return "TCK-1042"


with ants.start_as_current_span(name="support-request", agent_name="support_agent"):
    articles = search_kb("reset password")
    ticket = create_ticket("Password reset help")

ants.flush()
```

### 4. Calling models through the Budget AI gateway

Point the traced OpenAI client at your Budget AI gateway with a gateway key. The gateway enforces budgets and limits, and the call is recorded once, inside your agent's trace:

```python
from ants_platform.openai import openai

client = openai.OpenAI(
    base_url="https://<your-gateway-host>/v1",
    api_key="sk-ants-...",
)

with ants.start_as_current_span(name="support-request", agent_name="support_agent"):
    client.chat.completions.create(
        model="<model-group>",
        messages=[{"role": "user", "content": "Hello"}],
    )
```

## Learn more

- [Quick start](https://www.antsplatform.ai/docs/getting-started/quick-start): from a new account to your first trace
- [Understanding AI agents](https://www.antsplatform.ai/docs/core-concepts/ai-agents): how traces are attributed to agents
- [Authentication](https://www.antsplatform.ai/docs/getting-started/authentication): API keys and hosts
- [Full documentation](https://www.antsplatform.ai/docs)

## License

MIT
