Skip to main content

ai-infra

Build AI applications in minutes, not months.

PyPI CI Python License

Overview

One unified SDK for LLMs, agents, RAG, voice, images, and MCP—across 10+ providers.

Key Features

  • LLM Chat - Chat, streaming, structured output, retries across providers
  • Agents - Tool calling, human-in-the-loop, deep research mode
  • RAG - Embeddings, vector stores, retrieval pipelines
  • MCP - Client/server, OpenAPI->MCP conversion, tool discovery
  • Voice - Text-to-speech, speech-to-text, realtime conversations
  • Tracing - OpenTelemetry distributed tracing built-in

Why ai-infra?

Building AI apps means juggling OpenAI, Anthropic, Google, embeddings, vector stores, tool calling, MCP servers... each with different APIs and gotchas.

ai-infra gives you one clean interface that works everywhere:

from ai_infra import Agent

def search_web(query: str) -> str:
    """Search the web."""
    return f"Results for: {query}"

agent = Agent(tools=[search_web])
result = agent.run("Find the latest news about AI")
# Works with OpenAI, Anthropic, Google—same code.

Quick Install

pip install ai-infra

What's Included

Feature What You Get One-liner
LLM Chat Chat, streaming, structured output, retries LLM().chat("Hello")
Agents Tool calling, human-in-the-loop, deep mode Agent(tools=[...]).run(...)
RAG Embeddings, vector stores, retrieval Retriever().search(...)
MCP Client/server, OpenAPI->MCP, tool discovery MCPClient(url)
Voice Text-to-speech, speech-to-text, realtime TTS().speak(...)
Images DALL-E, Stability, Imagen generation ImageGen().generate(...)
Graph LangGraph workflows, typed state Graph().add_node(...)
Memory Context fitting, rolling summaries fit_context(messages, max_tokens=4000)
Workspace Sandboxed file operations for agents Workspace("./project")
Validation Prompt injection, PII detection validate_prompt(input)
Tracing OpenTelemetry distributed tracing configure_tracing(...)

30-Second Examples

Chat with any LLM

from ai_infra import LLM

llm = LLM()  # Uses OPENAI_API_KEY by default
response = llm.chat("Explain quantum computing in one sentence")
print(response)

# Switch providers instantly
llm = LLM(provider="anthropic", model="claude-sonnet-4-20250514")
response = llm.chat("Same question, different model")

Build an Agent with Tools

from ai_infra import Agent

def get_weather(city: str) -> str:
    """Get current weather for a city."""
    return f"72F and sunny in {city}"

def search_web(query: str) -> str:
    """Search the web for information."""
    return f"Top results for: {query}"

agent = Agent(tools=[get_weather, search_web])
result = agent.run("What's the weather in Tokyo and find me restaurants there")
# Agent automatically calls both tools and synthesizes the answer

RAG in 5 Lines

from ai_infra import Retriever

retriever = Retriever()
retriever.add_file("company_docs.pdf")
retriever.add_file("product_manual.md")

results = retriever.search("How do I reset my password?")
print(results[0].content)

Connect to MCP Servers

from ai_infra import MCPClient

async with MCPClient("http://localhost:8080") as client:
    tools = await client.list_tools()
    result = await client.call_tool("search", {"query": "AI news"})

Create an MCP Server

from ai_infra import mcp_from_functions

def search_docs(query: str) -> str:
    """Search documentation."""
    return f"Found: {query}"

mcp = mcp_from_functions(name="my-mcp", functions=[search_docs])
mcp.run(transport="stdio")

Supported Providers

Provider Chat Embeddings TTS STT Images Realtime
OpenAI Yes Yes Yes Yes Yes Yes
Anthropic Yes - - - - -
Google Yes Yes Yes Yes Yes Yes
xAI (Grok) Yes - - - - -
ElevenLabs - - Yes - - -
Deepgram - - - Yes - -
Stability AI - - - - Yes -
Replicate - - - - Yes -
Voyage AI - Yes - - - -
Cohere - Yes - - - -

Setup

# Set your API keys (use whichever providers you need)
export OPENAI_API_KEY=sk-...
export ANTHROPIC_API_KEY=sk-ant-...
export GOOGLE_API_KEY=...

# That's it. ai-infra auto-detects available providers.

Feature Highlights

Deep Agent (Autonomous Mode)

For complex, multi-step tasks:

from ai_infra import DeepAgent

agent = DeepAgent(
    goal="Analyze this codebase and generate documentation",
    tools=[read_file, write_file, search],
    max_iterations=50,
)

result = await agent.run()
print(result.output)

Includes: Planning, self-correction, progress tracking, human approval gates.

MCP Client with Interceptors

Advanced MCP features:

from ai_infra import MCPClient
from ai_infra.mcp import RetryInterceptor, CachingInterceptor, LoggingInterceptor

async with MCPClient(
    "http://localhost:8080",
    interceptors=[
        RetryInterceptor(max_retries=3),
        CachingInterceptor(ttl=300),
        LoggingInterceptor(),
    ]
) as client:
    # Automatic retries, caching, and logging for all tool calls
    result = await client.call_tool("expensive_operation", {...})

Includes: Callbacks, interceptors, prompts, resources, progress tracking.

RAG with Multiple Backends

from ai_infra import Retriever

# In-memory (development)
retriever = Retriever(backend="memory")

# SQLite (local persistence)
retriever = Retriever(backend="sqlite", path="./vectors.db")

# PostgreSQL with pgvector (production)
retriever = Retriever(backend="postgres", connection_string="...")

# Pinecone (managed cloud)
retriever = Retriever(backend="pinecone", index_name="my-index")

Voice & Multimodal

from ai_infra import TTS, STT

# Text to speech
tts = TTS(provider="elevenlabs")
audio = tts.speak("Hello, world!")

# Speech to text
stt = STT(provider="deepgram")
text = stt.transcribe("audio.mp3")

Image Generation

from ai_infra import ImageGen

gen = ImageGen(provider="openai")  # or "stability", "replicate"
image = gen.generate("A futuristic city at sunset")
image.save("city.png")

CLI Tools

# Test MCP connections
ai-infra mcp test --url http://localhost:8080

# List MCP tools
ai-infra mcp tools --url http://localhost:8080

# Call an MCP tool
ai-infra mcp call --url http://localhost:8080 --tool search --args '{"query": "test"}'

# Server info
ai-infra mcp info --url http://localhost:8080

Documentation

Section Description
Getting Started Installation, API keys, first example
Core
LLM Chat, streaming, structured output
Agent Tool calling, human-in-the-loop
Graph LangGraph workflows
RAG & Embeddings
Retriever Vector search, file loading
Embeddings Text embeddings
MCP
Client Connect to MCP servers
Server Create MCP servers
Multimodal
TTS Text-to-speech
STT Speech-to-text
Vision Image understanding
Advanced
Deep Agent Autonomous agents
Personas Agent personalities
Workspace Sandboxed file operations
Memory Context management, rolling summaries
Streaming Typed streaming events
Infrastructure
Validation Prompt/response validation
Tracing OpenTelemetry tracing
Callbacks Execution hooks
CLI Reference Command-line tools

Running Examples

git clone https://github.com/nfraxlab/ai-infra.git
cd ai-infra
poetry install

# Chat
poetry run python -c "from ai_infra import LLM; print(LLM().chat('Hello!'))"

# Agent
poetry run python examples/agents/01_basic_tools.py

# See more examples
ls examples/

Related Packages

ai-infra is part of the nfrax infrastructure suite:

Package Purpose
ai-infra AI/LLM infrastructure (agents, tools, RAG, MCP)
svc-infra Backend infrastructure (auth, billing, jobs, webhooks)
fin-infra Financial infrastructure (banking, portfolio, insights)

License

MIT License - use it for anything.


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ai_infra-1.19.0.tar.gz (505.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ai_infra-1.19.0-py3-none-any.whl (630.4 kB view details)

Uploaded Python 3

File details

Details for the file ai_infra-1.19.0.tar.gz.

File metadata

  • Download URL: ai_infra-1.19.0.tar.gz
  • Upload date:
  • Size: 505.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for ai_infra-1.19.0.tar.gz
Algorithm Hash digest
SHA256 6c144bcb66dbe7f16f8fe1008f153e8b04428dd09f973e9636cd21dfa9d0c720
MD5 c74ab4916dbf7f5728f76a21d5266777
BLAKE2b-256 09de33ef82f39c17de6350fb9471bca217a052efb2d40e5b97e831194044ab8f

See more details on using hashes here.

File details

Details for the file ai_infra-1.19.0-py3-none-any.whl.

File metadata

  • Download URL: ai_infra-1.19.0-py3-none-any.whl
  • Upload date:
  • Size: 630.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for ai_infra-1.19.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1e8fbea85f2cd7eeb1d07f0bfe67d68d327e9d6e98ce250a7971c000b10e6977
MD5 346e430d21f9da4e7b8b2b9f0a8b052d
BLAKE2b-256 ecec257ae81d1ddd1fdaf02c3f075089cb587080e6a50d174caab1afa4d3ddd5

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.19.0 This release

2 files

1.18.0

2 files

1.17.0

2 files

1.16.0

2 files

1.15.0

2 files

1.14.0

2 files

1.13.0

2 files

1.12.0

2 files

1.11.0

2 files

1.10.0

2 files

1.9.1

2 files

1.9.0

2 files

1.8.1

2 files

1.8.0

2 files

1.7.0

2 files

1.6.0

2 files

1.5.2

2 files

1.5.1

2 files

1.5.0

2 files

1.4.0

2 files

1.3.0

2 files

1.2.0

2 files

1.1.2

2 files

1.1.1

2 files

1.1.0

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

0.1.171

2 files

0.1.170

2 files

0.1.169

2 files

0.1.168

2 files

0.1.167

2 files

0.1.166

2 files

0.1.165

2 files

0.1.164

2 files

0.1.163

2 files

0.1.162

2 files

0.1.161

2 files

0.1.160

2 files

0.1.159

2 files

0.1.158

2 files

0.1.157

2 files

0.1.156

2 files

0.1.155

2 files

0.1.154

2 files

0.1.153

2 files

0.1.152

2 files

0.1.151

2 files

0.1.150

2 files

0.1.149

2 files

0.1.148

2 files

0.1.147

2 files

0.1.146

2 files

0.1.145

2 files

0.1.144

2 files

0.1.143

2 files

0.1.142

2 files

0.1.141

2 files

0.1.140

2 files

0.1.139

2 files

0.1.138

2 files

0.1.137

2 files

0.1.136

2 files

0.1.135

2 files

0.1.134

2 files

0.1.133

2 files

0.1.132

2 files

0.1.131

2 files

0.1.130

2 files

0.1.129

2 files

0.1.128

2 files

0.1.127

2 files

0.1.126

2 files

0.1.125

2 files

0.1.124

2 files

0.1.123

2 files

0.1.122

2 files

0.1.121

2 files

0.1.120

2 files

0.1.119

2 files

0.1.118

2 files

0.1.117

2 files

0.1.116

2 files

0.1.115

2 files

0.1.114

2 files

0.1.112

2 files

0.1.111

2 files

0.1.109

2 files

0.1.108

2 files

0.1.107

2 files

0.1.106

2 files

0.1.105

2 files

0.1.104

2 files

0.1.103

2 files

0.1.102

2 files

0.1.101

2 files

0.1.100

2 files

0.1.99

2 files

0.1.98

2 files

0.1.97

2 files

0.1.96

2 files

0.1.95

2 files

0.1.94

2 files

0.1.93

2 files

0.1.92

2 files

0.1.91

2 files

0.1.90

2 files

0.1.89

2 files

0.1.88

2 files

0.1.87

2 files

0.1.86

2 files

0.1.85

2 files

0.1.84

2 files

0.1.83

2 files

0.1.82

2 files

0.1.81

2 files

0.1.80

2 files

0.1.79

2 files

0.1.78

2 files

0.1.77

2 files

0.1.76

2 files

0.1.75

2 files

0.1.74

2 files

0.1.73

2 files

0.1.72

2 files

0.1.71

2 files

0.1.70

2 files

0.1.69

2 files

0.1.68

2 files

0.1.67

2 files

0.1.66

2 files

0.1.65

2 files

0.1.64

2 files

0.1.63

2 files

0.1.62

2 files

0.1.61

2 files

0.1.60

2 files

0.1.59

2 files

0.1.58

2 files

0.1.57

2 files

0.1.56

2 files

0.1.55

2 files

0.1.54

2 files

0.1.53

2 files

0.1.52

2 files

0.1.51

2 files

0.1.50

2 files

0.1.49

2 files

0.1.48

2 files

0.1.47

2 files

0.1.46

2 files

0.1.45

2 files

0.1.44

2 files

0.1.43

2 files

0.1.42

2 files

0.1.41

2 files

0.1.40

2 files

0.1.39

2 files

0.1.38

2 files

0.1.37

2 files

0.1.36

2 files

0.1.35

2 files

0.1.34

2 files

0.1.33

2 files

0.1.32

2 files

0.1.31

2 files

0.1.30

2 files

0.1.29

2 files

0.1.28

2 files

0.1.27

2 files

0.1.26

2 files

0.1.25

2 files

0.1.24

2 files

0.1.23

2 files

0.1.22

2 files

0.1.21

2 files

0.1.20

2 files

0.1.19

2 files

0.1.18

2 files

0.1.17

2 files

0.1.16

2 files

0.1.15

2 files

0.1.14

2 files

0.1.13

2 files

0.1.12

2 files

0.1.11

2 files

0.1.10

2 files

0.1.9

2 files

0.1.7

2 files

0.1.6

2 files

0.1.5

2 files

0.1.3

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page