Metadata-Version: 2.4
Name: adeptiv-ai-gateway
Version: 0.1.0
Summary: Adeptiv gateway: check an app's model, tool and MCP calls against its containment rules before they run
Author-email: Adeptiv AI <contact@adeptiv-ai.com>
License: Apache-2.0
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Requires-Dist: opentelemetry-api
Requires-Dist: opentelemetry-sdk
Requires-Dist: websockets<17,>=13
Provides-Extra: openai
Requires-Dist: openinference-instrumentation-openai; extra == "openai"
Provides-Extra: anthropic
Requires-Dist: openinference-instrumentation-anthropic; extra == "anthropic"
Provides-Extra: litellm
Requires-Dist: openinference-instrumentation-litellm; extra == "litellm"
Provides-Extra: langchain
Requires-Dist: openinference-instrumentation-langchain; extra == "langchain"
Provides-Extra: llama-index
Requires-Dist: openinference-instrumentation-llama-index; extra == "llama-index"
Provides-Extra: vertexai
Requires-Dist: openinference-instrumentation-vertexai; extra == "vertexai"
Provides-Extra: bedrock
Requires-Dist: openinference-instrumentation-bedrock; extra == "bedrock"
Provides-Extra: google-genai
Requires-Dist: openinference-instrumentation-google-genai; extra == "google-genai"
Provides-Extra: mistralai
Requires-Dist: openinference-instrumentation-mistralai; extra == "mistralai"
Provides-Extra: groq
Requires-Dist: openinference-instrumentation-groq; extra == "groq"
Provides-Extra: dspy
Requires-Dist: openinference-instrumentation-dspy; extra == "dspy"
Provides-Extra: crewai
Requires-Dist: openinference-instrumentation-crewai; extra == "crewai"
Provides-Extra: google-adk
Requires-Dist: openinference-instrumentation-google-adk; extra == "google-adk"
Provides-Extra: haystack
Requires-Dist: openinference-instrumentation-haystack; extra == "haystack"
Provides-Extra: smolagents
Requires-Dist: openinference-instrumentation-smolagents; extra == "smolagents"
Provides-Extra: autogen
Requires-Dist: openinference-instrumentation-autogen; extra == "autogen"
Provides-Extra: instructor
Requires-Dist: openinference-instrumentation-instructor; extra == "instructor"
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: pytest-asyncio; extra == "test"
Provides-Extra: all
Requires-Dist: openinference-instrumentation-openai; extra == "all"
Requires-Dist: openinference-instrumentation-anthropic; extra == "all"
Requires-Dist: openinference-instrumentation-litellm; extra == "all"
Requires-Dist: openinference-instrumentation-langchain; extra == "all"
Requires-Dist: openinference-instrumentation-llama-index; extra == "all"
Requires-Dist: openinference-instrumentation-vertexai; extra == "all"
Requires-Dist: openinference-instrumentation-bedrock; extra == "all"
Requires-Dist: openinference-instrumentation-google-genai; extra == "all"
Requires-Dist: openinference-instrumentation-mistralai; extra == "all"
Requires-Dist: openinference-instrumentation-groq; extra == "all"
Requires-Dist: openinference-instrumentation-dspy; extra == "all"
Requires-Dist: openinference-instrumentation-crewai; extra == "all"
Requires-Dist: openinference-instrumentation-google-adk; extra == "all"
Requires-Dist: openinference-instrumentation-haystack; extra == "all"
Requires-Dist: openinference-instrumentation-smolagents; extra == "all"
Requires-Dist: openinference-instrumentation-autogen; extra == "all"
Requires-Dist: openinference-instrumentation-instructor; extra == "all"

# adeptiv-ai-gateway

Checks an AI app's model, tool and MCP calls against its Adeptiv
containment rules **before they run**. The app keeps making its own calls;
the gateway only lets a call go on, makes it wait its turn, or refuses it.

```bash
pip install "adeptiv-ai-gateway[openai]"      # plus the extra of each library you use
export ADEPTIV_API_KEY=...                    # the use case's key
export ADEPTIV_ENDPOINT=wss://...             # Adeptiv's WebSocket
```

```python
from adeptiv_gateway import Adeptiv, ContainmentError

client = Adeptiv()          # once, at start-up

try:
    reply = run_my_agent(question)          # unchanged code
except ContainmentError as exc:
    reply = exc.fallback_message            # what the rule says to tell the user
```

## What is checked

| Rule (set in Adeptiv) | A call it forbids raises |
|---|---|
| Pause / kill an agent | `AgentPaused` |
| Pause a model (apps with no agents) | `ModelPaused` |
| Approved models only | `ModelNotApproved` |
| Block a tool | `ToolBlocked` |
| Slow down | waits its turn; `RateLimited` past the longest wait |
| Budget (requests, tokens, cost) | `BudgetExceeded` |

Every error is a `ContainmentError` and carries `agent`, `event_id` and
`fallback_message`. With no rule configured, nothing is refused.

## Where calls are checked

| Libraries | How |
|---|---|
| OpenAI (and Azure), Anthropic, Google GenAI, Groq, Mistral, LiteLLM, Bedrock | their call methods, before the request is sent (sync and async) |
| LangChain / LangGraph, OpenAI Agents SDK, CrewAI, Google ADK, LlamaIndex | their own hooks |
| Vertex AI, DSPy, Haystack, smolagents, AutoGen, Instructor | their OpenInference span, as it starts (install the library's extra) |
| MCP | `ClientSession.call_tool` |

Budget tokens are read from the model calls' OpenInference spans, so
install the extra of each model client you use (`[openai]`, `[anthropic]`,
...).

"Slow down" makes a model or MCP call wait its turn (an async call awaits
it). A framework's own steps are not paced, since their hooks cannot wait
without stopping an event loop; their model calls are.

## Which agent a call belongs to

The framework's agent (LangGraph node, CrewAI role, Agents SDK agent...),
else the agent named around it:

```python
with client.agent("Planner"):
    plan = openai_client.chat.completions.create(...)
```

else `Adeptiv(agent="...")` / `ADEPTIV_AGENT`.

## Code no hook covers

```python
for call in response.tool_calls:
    client.check_tool(call.name)            # or: await client.check_tool_async(...)
    run(call)

client.check_model("gpt-4o")                # a model called over plain HTTP
client.record_usage("gpt-4o", input_tokens=812, output_tokens=240)

if client.is_paused("Planner"):
    ...
```

## With the Adeptiv tracing SDK

Both work together in any order: the checks go on the SDK's tracer
provider, so its traces keep every span.

## Settings

| Argument | Environment variable | Default |
|---|---|---|
| `api_key` | `ADEPTIV_API_KEY` | required |
| `endpoint` | `ADEPTIV_ENDPOINT` | required |
| `agent` | `ADEPTIV_AGENT` | none |
| `enabled` | `ADEPTIV_CONTAINMENT` | on |
| | `ADEPTIV_CONTAINMENT_INITIAL_WAIT` | 1.0 s (first check waits for the rules) |
| | `ADEPTIV_CONTAINMENT_MAX_WAIT` | 10.0 s (longest slow-down wait) |
