Portkey Integration

Purple Flea for Portkey

Add Portkey's enterprise-grade AI gateway to your Purple Flea agents. Get full observability, guardrails against rogue trades, LLM fallbacks, semantic caching, and audit logs for every financial decision your agent makes.

Get Free API Key → View Docs

Why Agents Need an AI Gateway

An autonomous trading or gambling agent making unobserved decisions is a liability. Portkey solves this.

👁️
Full Observability
See every LLM call your agent makes before executing a trade, placing a bet, or sending crypto. Log inputs, outputs, latencies, and costs in a central dashboard.
🛡️
Financial Guardrails
Define rules that prevent dangerous actions: "never trade with more than 10% of balance", "no leveraged positions after 3 losses", "flag any single bet over 0.1 ETH".
🔄
LLM Fallbacks
If your primary LLM (GPT-4o) is down or slow, Portkey automatically falls back to Claude or Gemini. Your trading agent stays operational 24/7.
Semantic Caching
Cache similar market analysis queries. "What's the BTC trend?" doesn't need a fresh LLM call every 30 seconds — Portkey reuses recent responses when appropriate.
📊
Cost Tracking
Track LLM costs alongside Purple Flea trading fees. Understand the true cost of each agent decision: LLM tokens + transaction fees combined.
🔐
Virtual Keys
Issue separate virtual API keys for each agent instance. Revoke a compromised agent's keys without affecting other agents or your master Purple Flea credentials.

Setup: Portkey + Purple Flea

Add an AI gateway to your crypto agent in 10 minutes.

Install & Configure

pip install portkey-ai openai purpleflea from portkey_ai import Portkey # Portkey as a drop-in for OpenAI client = Portkey( api_key="YOUR_PORTKEY_KEY", virtual_key="OPENAI_VIRTUAL_KEY" ) # Purple Flea tools (function calling format) import purpleflea as pf pf_client = pf.Client(api_key="YOUR_PF_KEY")

Add Guardrails for Trading

from portkey_ai import Portkey, Config, Guardrails # Define financial safety guardrails guardrails = Guardrails( on_request=[ { "id": "no-large-bets", "type": "regex", "config": { "pattern": r'"size":\s*([5-9]\d+|\d{3,})', "action": "block", "message": "Position size too large, capped at 4" } } ] ) config = Config( provider="openai", api_key="...", guardrails=guardrails, cache={"mode": "semantic"} ) client = Portkey(config=config)

Observable Trading Agent

import json, purpleflea as pf pf_client = pf.Client(api_key="YOUR_PF_KEY") trading_tools = [ {"type": "function", "function": { "name": "open_trade", "description": "Open perpetual futures position", "parameters": { "type": "object", "properties": { "symbol": {"type": "string"}, "side": {"type": "string", "enum": ["long", "short"]}, "size": {"type": "number"}, "leverage": {"type": "integer"} }, "required": ["symbol", "side", "size"] } }} ] # Every call logged by Portkey, guardrails enforced response = client.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": "Trade BTC"}], tools=trading_tools, metadata={"agent_id": "trader-001", "session": "morning-run"} ) # Handle tool calls with Purple Flea for tc in response.choices[0].message.tool_calls or []: args = json.loads(tc.function.arguments) result = pf_client.trading.open_trade(**args) print(f"Trade opened: {result['position_id']}")

Multi-Provider Fallback

from portkey_ai import Portkey, Config # Primary: GPT-4o. Fallback: Claude. Final: Gemini. config = Config( strategy={"mode": "fallback"}, targets=[ { "provider": "openai", "virtual_key": "openai-vkey" }, { "provider": "anthropic", "virtual_key": "claude-vkey" }, { "provider": "google", "virtual_key": "gemini-vkey" } ] ) client = Portkey(api_key="pk-...", config=config) # Your agent keeps trading even if GPT-4o is down response = client.chat.completions.create( model="gpt-4o", # Portkey handles fallback automatically messages=[{"role": "user", "content": "Analyze BTC"}], tools=trading_tools )

Build Observable, Safe Crypto Agents

Combine Purple Flea's financial APIs with Portkey's AI gateway for production-grade autonomous agents.