Smartflow SDK Reference

Smartflow SDK for Python — Developer Reference

Smartflow SDK

Python · v0.3.1

Smartflow SDK for Python

Developer Reference

v0.3.1 Python 3.9+ pip install smartflow-sdk

What is Smartflow?

Smartflow is an enterprise AI orchestration layer that sits between your application and AI providers (OpenAI, Anthropic, Google, and others). It provides:

  • Intelligent Routing — Route requests to the best provider based on cost, latency, and availability

  • 3-Layer Semantic Cache — 60–80% cost reduction with L1/L2/L3 caching and semantic similarity matching

  • ML-Powered Compliance — Real-time PII detection with adaptive learning and behavioral analysis

  • Complete Audit Trail — Every AI interaction logged for compliance, debugging, and analytics

  • Automatic Failover — Zero-downtime provider switching when issues occur

  • MCP Tool Gateway — Register and invoke external MCP tools with shared auth, budgeting, and audit

  • A2A Agent Orchestration — Route tasks to registered A2A agents with full traceability

YOUR APPLICATION │ pip install smartflow-sdk │ ┌────────▼────────┐ │ Smartflow SDK │ └────────┬────────┘ │ ┌────────▼──────────────────────┐ │ SMARTFLOW PROXY │ │ MetaCache · ML Compliance │ │ Routing · VAS Logging │ │ MCP Gateway · A2A Gateway │ └────────┬──────────────────────┘ │ ┌─────┼─────┬─────┬──────┐ ▼ ▼ ▼ ▼ ▼ OpenAI Anthropic Gemini Cohere Local

Installation

From source:

Optional — sync client in async environments:

Quick Start

Synchronous — Scripts and Notebooks

For Jupyter notebooks with an existing event loop:

OpenAI Drop-in Replacement

Zero code changes required — just update the base URL:

SmartflowClient

Primary async client.

SmartflowClient( base_url: str, # Proxy URL, e.g. "http://smartflow:7775" api_key: Optional[str] = None, # Virtual key (sk-sf-...) timeout: float = 30.0, # Request timeout in seconds management_port: int = 7778, # Management API port compliance_port: int = 7777, # Compliance API port bridge_port: int = 3500, # Hybrid bridge port )

chat()

Send a message, receive the reply as a plain string.

chat(message, model="gpt-4o", system_prompt=None, temperature=0.7, max_tokens=None, **kwargs) → str

chat_completions()

Full OpenAI-compatible completions. Returns a structured AIResponse.

chat_completions(messages, model="gpt-4o", temperature=0.7, max_tokens=None, stream=False, **kwargs) → AIResponse

stream_chat()

Async generator that yields text delta strings as they stream.

stream_chat(message, model="gpt-4o", system_prompt=None, temperature=0.7, max_tokens=None, **kwargs) → AsyncIterator[str]

claude_message()

Send a message to Claude using the native Anthropic Messages API path. The proxy injects the API key automatically.

claude_message(message, model="claude-3-5-sonnet-20241022", max_tokens=1024, system=None, anthropic_key=None) → str

Routes to /anthropic/v1/messages. For multi-turn or multimodal use, call chat_completions() with the Claude model name.

embeddings()

Generate vector embeddings. Supports batch input and provider routing.

embeddings(input, model="text-embedding-3-small", encoding_format="float", dimensions=None, input_type=None, **kwargs) → Dict

image_generation()

image_generation(prompt, model="dall-e-3", n=1, size="1024x1024", quality=None, response_format="url", style=None, **kwargs) → Dict

audio_transcription()

Transcribe audio. Accepts a file-like object.

audio_transcription(file, model="whisper-1", language=None, prompt=None, response_format="json", temperature=0.0, filename="audio.mp3", **kwargs) → Dict

text_to_speech()

Synthesize speech. Returns raw audio bytes.

text_to_speech(input, model="tts-1", voice="alloy", response_format="mp3", speed=1.0, **kwargs) → bytes

rerank()

Rerank documents by relevance to a query.

rerank(query, documents, model="rerank-english-v3.0", top_n=None, **kwargs) → Dict

list_models()

chatbot_query()

Query Smartflow's built-in system chatbot for operational information using natural language.

chatbot_query(query: str) → Dict

Provider Prefix Reference

All methods that accept a model parameter support provider prefix routing. The three primary providers are auto-detected from model name.

Automatic Detection (no prefix needed)

Explicit Prefix Required

Prefix
Provider
API Key Env Var

(none)

OpenAI

OPENAI_API_KEY

anthropic/

Anthropic

ANTHROPIC_API_KEY

xai/

xAI (Grok)

XAI_API_KEY

gemini/

Google Gemini

GEMINI_API_KEY

vertex_ai/

Google Vertex AI

VERTEXAI_API_KEY

openrouter/

OpenRouter

OPENROUTER_API_KEY

azure/

Azure OpenAI

AZURE_API_KEY + AZURE_API_BASE

mistral/

Mistral AI

MISTRAL_API_KEY

cohere/

Cohere

COHERE_API_KEY

nvidia_nim/

NVIDIA NIM

NVIDIA_NIM_API_KEY

huggingface/

HuggingFace

HUGGINGFACE_API_KEY

groq/

Groq

GROQ_API_KEY

deepgram/

Deepgram

DEEPGRAM_API_KEY

fireworks/

Fireworks AI

FIREWORKS_API_KEY

novita/

Novita AI

NOVITA_API_KEY

together/

Together AI

TOGETHER_API_KEY

perplexity/

Perplexity AI

PERPLEXITY_API_KEY

replicate/

Replicate

REPLICATE_API_KEY

vercel_ai_gateway/

Vercel AI Gateway

VERCEL_AI_GATEWAY_API_KEY

ollama/

Ollama (local)

none required

Intelligent Compliance Engine

Smartflow's ML-powered compliance engine goes beyond regex. It learns and adapts based on user behavior and organizational baselines.

┌─────────────────────────────────────────────────────────────┐ │ INTELLIGENT COMPLIANCE │ ├─────────────────────────────────────────────────────────────┤ │ │ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ │ │ LAYER 1 │ │ LAYER 2 │ │ LAYER 3 │ │ │ │ Regex/Rules │→ │ ML Embeddings│→ │ Behavioral │ │ │ │ │ │ │ │ Analysis │ │ │ │ SSN │ │ Semantic │ │ User │ │ │ │ Credit Card │ │ similarity │ │ patterns │ │ │ │ Email │ │ Context │ │ Org │ │ │ │ Phone │ │ awareness │ │ baselines │ │ │ │ MRN │ │ Learned │ │ Anomaly │ │ │ │ Passport │ │ patterns │ │ detection │ │ │ └──────────────┘ └──────────────┘ └──────────────┘ │ │ └──────────────┬───────────────┘ │ │ ▼ │ │ ┌──────────────────┐ │ │ │ CORRELATION │ │ │ │ ENGINE │ │ │ │ Composite Risk │ │ │ │ Score + Action │ │ │ └──────────────────┘ │ │ ▼ │ │ Allow | AllowAndLog | Review | Block │ └─────────────────────────────────────────────────────────────┘

intelligent_scan()

intelligent_scan(content, user_id=None, org_id=None, context=None) → IntelligentScanResult

Enable behavioral analysis:

check_compliance()

check_compliance(content, policy="enterprise_standard") → ComplianceResult

redact_pii()

submit_compliance_feedback()

Submit a true/false-positive correction to retrain the ML model.

submit_compliance_feedback(scan_id, is_false_positive, user_id=None, notes=None) → Dict

Learning & ML Methods

Method
Returns
Description

get_org_summary()

Dict

Organization-level aggregate compliance stats

get_org_baseline(org_id)

OrgBaseline

Behavioral baseline used for anomaly detection

get_persistence_stats()

PersistenceStats

Redis persistence stats for compliance data

save_compliance_data()

Dict

Trigger manual flush of compliance data to Redis

get_intelligent_health()

Dict

Health status of ML engine and embedding service

MCP Tool Invocation

MCP tool calls are direct HTTP requests to the proxy. Use httpx or any HTTP client.

Calling a Tool

Searching the Tool Catalog

MCP Usage and Cost

A2A Agent Invocation

A2A tasks are HTTP POST requests to the proxy. The proxy forwards to the registered agent, logs the exchange, and returns the result.

Sending a Task

Agent Capability Card

The x-a2a-trace-id header is passed through all hops in a multi-agent chain so every agent's logs can be correlated by a single trace ID.

Cache Performance

Provider Health

System Health

Audit Logs (VAS)

get_logs(limit=50, provider=None) → List[VASLog]

get_logs_hybrid()

Unified audit log across all Smartflow instances via the hybrid bridge.

Analytics

get_analytics(start_date=None, end_date=None) → Dict

Routing Control

SmartflowAgent

Higher-level agent with conversation memory, compliance scanning, and tool support.

Method / Property
Description

chat(message, scan_input=True, scan_output=True)

Send message; raises ComplianceError if blocked

clear_history()

Reset conversation, preserve system prompt

get_history()

Return copy of message history

message_count

Number of messages in history

SmartflowWorkflow

Chain AI operations with branching logic.

Step Action
Config fields
Description

"chat"

prompt, model, temperature

Chat completion; {input} / {output} are template variables

"compliance_check"

content

Rule-based compliance scan

"condition"

field, cases, default

Branch on a context value

SyncSmartflowClient

Synchronous wrapper. Every async method is available without await.

Configuration

Client Options

From Environment Variables

Error Handling

Exception
Condition

SmartflowError

Base class for all SDK errors

ConnectionError

Cannot connect to proxy

AuthenticationError

401 — invalid or missing key

RateLimitError

429 — rate limit hit

ComplianceError

403 — request blocked by compliance policy

ProviderError

Upstream provider error

TimeoutError

Request timeout

Response Types

AIResponse

Field
Type
Description

content

str

First choice text

choices

list

Full choices array

usage

Usage

Token usage (prompt_tokens, completion_tokens, total_tokens)

model

str

Model used

id

str

Response ID

cached

bool

True if served from MetaCache

cache_hit_type

str

"exact", "semantic", or None

provider

str

Provider that served the request

CacheStats

Field
Type

hit_rate

float

hits / misses

int

l1_hits / l2_hits / l3_hits

int

tokens_saved

int

cost_saved_cents

int

entries

int

ComplianceResult

Field
Type

has_violations

bool

compliance_score

float

violations

list[str]

pii_detected

list[str]

risk_level

str"low" / "medium" / "high" / "critical"

recommendations

list[str]

redacted_content

str | None

IntelligentScanResult

Field
Type

has_violations

bool

risk_score

float — 0.0 to 1.0

recommended_action

str"Allow" / "AllowAndLog" / "Review" / "Block"

explanation

str

regex_violations

list

ml_violations

list

behavior_deviations

list

processing_time_us

int

VASLog

Field
Type

request_id

str

timestamp

str

provider

str

model

str

tokens_used

int

latency_ms

float

cached

bool

compliance

ComplianceResult | None

ProviderHealth

Field
Type

provider

str

status

str"healthy" / "degraded" / "unhealthy"

latency_ms

float

success_rate

float

error_rate

float

requests_total

int

last_updated

str

SystemHealth

Field
Type

status

str

uptime_seconds

int

version

str

providers

dict

cache

dict

timestamp

str

WorkflowResult

Field
Type

success

bool

output

str

steps_executed

list[str]

errors

list

total_tokens

int

total_cost_cents

int

execution_time_ms

float

Use Case 1: Secure Customer Support Bot

Challenge: Build a customer support chatbot that handles sensitive information while maintaining PCI-DSS and GDPR compliance.

What this demonstrates: PII detection blocks sensitive data before it reaches the AI provider · Behavioral tracking learns normal patterns per customer · Complete audit trail for compliance audits · Graceful handling of compliance violations

Use Case 2: Cost-Optimized Content Generation Pipeline

Challenge: Generate thousands of product descriptions daily while minimizing API costs.

What this demonstrates: Semantic caching recognizes similar products and reuses responses · Structured prompts maximize cache hit potential · Real-time cost tracking via cost_saved_cents

Use Case 3: Multi-Agent Research and Report Generation

Challenge: Coordinate multiple specialized AI agents to produce a polished, auditable research report.

What this demonstrates: Coordinated multi-agent workflows with specialized roles · Progressive refinement through an agent chain · Complete per-request audit trail · Organizational context tracking for behavioral analysis

Summary

Feature
Benefit

Semantic Cache (3-tier)

60–80% cost reduction, no external vector DB

ML Compliance Engine

Real-time PII protection with adaptive learning

Smart Routing

Latency, cost, or priority-based provider selection

Full Audit Trail (VAS)

Complete compliance visibility across every request

MCP Tool Gateway

Register and invoke external tools with shared auth and budgeting

A2A Agent Orchestration

Route tasks across agents with full traceability

Agent Builder

Production-ready conversational AI with memory and compliance

Workflow Orchestration

Multi-step AI pipelines with branching and error handling

Resources & Support

Changelog

v0.3.1 — 2026

  • Added chatbot_query() — natural-language operational queries

  • Added get_logs_hybrid() — unified audit log across all instances via hybrid bridge

  • Added submit_compliance_feedback() — true/false-positive corrections for ML retraining

  • Added get_learning_status(), get_learning_summary() — adaptive learning progress

  • Added get_ml_stats() — ML engine pattern counts and accuracy

  • Added get_org_summary(), get_org_baseline() — organizational compliance baselines

  • Added get_persistence_stats(), save_compliance_data(), get_intelligent_health()

  • Documented MCP tool invocation and catalog search patterns

  • Documented A2A task invocation and capability card retrieval

  • Corrected field names: latency_ms (not avg_latency_ms), cost_saved_cents (not cost_saved_usd)

  • Corrected get_analytics() signature: start_date/end_date parameters

  • Corrected SystemHealth field names: status, uptime_seconds, providers

v0.3.0

  • Added image_generation(), audio_transcription(), text_to_speech(), stream_chat(), rerank()

  • Extended embeddings() with encoding_format, dimensions, input_type

  • New providers: Groq, Deepgram, Fireworks AI

v0.2.0

  • Added SmartflowAgent with compliance scanning and conversation memory

  • Added SmartflowWorkflow for multi-step AI pipelines

  • Added intelligent_scan(), submit_compliance_feedback()

  • Added get_provider_health(), get_cache_stats(), health_comprehensive()

v0.1.0

  • Initial release: chat(), chat_completions(), embeddings(), claude_message()

  • VAS audit logging, SyncSmartflowClient


© 2026 Langsmart, Inc. All rights reserved. Smartflow is a trademark of Langsmart, Inc.