Metadata-Version: 2.5
Name: aiwarden-sdk
Version: 0.1.0
Summary: Official Python client for aiwarden — AI governance, threat detection, and agent oversight
Project-URL: Homepage, https://platform.aiwarden.ai
Project-URL: Documentation, https://platform.aiwarden.ai/documentation
Author: aiwarden
License: MIT
License-File: LICENSE
Keywords: agents,ai,governance,guardrails,llm,prompt-injection,security
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Security
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.9
Requires-Dist: httpx>=0.27
Requires-Dist: pydantic>=2.0
Provides-Extra: dev
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
Requires-Dist: pytest>=8; extra == 'dev'
Requires-Dist: respx>=0.21; extra == 'dev'
Requires-Dist: ruff<0.7,>=0.6; extra == 'dev'
Description-Content-Type: text/markdown

# aiwarden — Python SDK

Official Python client for [aiwarden](https://platform.aiwarden.ai) — AI governance, threat detection, and agent oversight.

> Status: alpha. API surface may change before `1.0.0`.

## Install

```bash
pip install aiwarden-sdk
```

## Quickstart

```python
from aiwarden import Aiwarden

client = Aiwarden(api_key="awd_live_...")

# Per-request analysis (regex + AI threat detection, attachments supported)
verdict = client.analyze(prompt="Ignore all previous instructions")
print(verdict.action)  # "block"

# Full agent governance (policies, rate limits, MCP, approvals, lineage)
result = client.agents.execute(
    agent_id="my-foundry-agent",
    action_type="llm_call",
    prompt="Summarize this customer ticket",
    user={"email": "alice@acme.com"},
)
if result.action == "block":
    raise RuntimeError(result.reason)
```

## Agentic Endpoint: aiwarden inside your agent

Install the package in the agent's environment and it governs the whole agent:
every model call and every tool it is about to run is checked with aiwarden
first, and the verdict is applied (allow, block, hold for approval).

```bash
pip install aiwarden-sdk
export AIWARDEN_INSTALL_CODE=aep_...
export AIWARDEN_AGENT_ID=billing-agent      # optional; defaults to <script>@<host>
```

The install code (created in the portal, Integrations → Agentic Endpoint)
expires and has a limit of uses. On its first start the agent swaps it for a
credential of its own, kept in `~/.aiwarden` (owner-only), so your account key
never reaches the machine the agent runs on. An agent without a disk has
nowhere to keep it: start that one with `AIWARDEN_API_KEY=<your agents key>`
instead.

One line at the top of the agent:

```python
import aiwarden.endpoint as endpoint
endpoint.install()
```

Or no code change at all:

```bash
aiwarden-run python my_agent.py
```

What it attaches to (verified against the real libraries in `tests/test_endpoint.py`
and `tests/test_endpoint_frameworks.py`):

| Where | What is checked |
|---|---|
| OpenAI client (`chat.completions`, `responses`) | the person's message, the tools the model asks for, and tool results before the model reads them |
| Anthropic client (`messages`) | same |
| LangChain / LangGraph tools | the moment a tool runs, and its output |
| OpenAI Agents SDK (`openai-agents`) function tools | the moment a tool runs, and its output (`Runner.run` and `Runner.run_streamed`, Responses and Chat Completions) |
| CrewAI tools | the moment a tool runs, and its output (native tool calls and ReAct, any model provider) |
| Your own functions | `@endpoint.guarded` |

Because it hooks the model client itself, it also covers agents written with
plain API calls and any framework that calls OpenAI or Anthropic through their
SDKs.

A blocked tool never runs. By default the model is told with a neutral message
and the agent goes on (`AIWARDEN_ON_BLOCK=replace`); with `raise`,
`aiwarden.endpoint.AiwardenBlocked` is raised. `endpoint.set_user(email)` tells
aiwarden on whose behalf the agent is acting. `endpoint.status()` shows what is
attached and how many actions were checked, blocked, or let through without a
verdict.

Fail-open: if aiwarden cannot be reached, the agent keeps working and the
action is counted as `ungoverned`.

Streaming is governed too (`create(stream=True)` in both clients): text flows
as it arrives and only the events that ask for a tool are held until aiwarden
answers.

What the model answers is sent back to aiwarden off the agent's path (text of
the last answer of a turn, real tokens, time of the call), so the agent's cost
and its answers show up in aiwarden. When the policy's containment can change
an answer (retry once, hold or close the session), the package waits for
aiwarden's verdict on it; otherwise nothing waits. A short-lived agent should
call `endpoint.flush()` before exiting.

Known limits: the `.stream()` helpers of both SDKs (`chat.completions.stream`,
`responses.stream`, `messages.stream`) are checked on the request side only
(`create(stream=True)` is fully governed in the three APIs); a blocked tool
call in the non-streamed Responses API always raises (not inside the OpenAI
Agents SDK, where the tool is decided when it runs); answers asked for through
`with_raw_response` / `with_streaming_response` are checked on the request side
only (CrewAI's OpenAI provider does this: its tools are governed by the CrewAI
hook, but that model's answers and tokens are not reported); MCP clients do not
have their own hook yet. In the OpenAI Agents SDK, handoffs are checked at the
model only, and hosted, shell, computer and custom tools are not checked. In
CrewAI, the model call is governed only for the providers that use the OpenAI
or Anthropic SDK; `on_block=raise` cannot leave a CrewAI hook, so a denied tool
is blocked and the model told; and a crew that clears CrewAI's tool hooks
removes aiwarden's too.

## What this SDK covers (v0.1)

- `client.analyze()` — `POST /analyze` (per-request, supports text + attachments).
- `client.ask()` — `POST /ask` (full proxy LLM flow with detection + intel logging).
- `client.agents.execute()` — `POST /api/agent/v1/execute` (full agent governance pipeline).
- `client.agents.register()` — `POST /api/agent/v1/register`.
- `client.agents.list_tools()` — `GET /api/agent/v1/tools`.
- `client.agents.status()` — `GET /api/agent/v1/status`.

## Fail mode

By default the SDK is **fail-open**: if aiwarden times out or errors, your agent continues without governance (a `WARN` is logged). Switch to strict for regulated workloads:

```python
client = Aiwarden(api_key="awd_live_...", fail_mode="closed", timeout=2.0)
```

### Latency budget

`timeout` is **per attempt**. On a transient failure (network, 5xx, 429) the SDK
retries — 3 times by default — with exponential backoff capped at 8s. So with an
unreachable aiwarden the caller can wait on the order of **ten seconds** before
getting the fail-open verdict, not the two the timeout suggests.

If your path is latency-sensitive, cap it:

```python
client = Aiwarden(api_key="awd_live_...", retries=0)   # answers on the first timeout
```

## Async

```python
from aiwarden import AsyncAiwarden

async with AsyncAiwarden(api_key="awd_live_...") as client:
    verdict = await client.analyze(prompt="...")
```

## Documentation

Full reference + integration guides at [platform.aiwarden.ai/documentation](https://platform.aiwarden.ai/documentation).

## License

MIT
