WhiteBoxAI SDK supports offline mode for robust operation when network connectivity is unreliable.
Offline mode provides:
- Persistent Queue: SQLite-based storage that survives application restarts
- Auto-Sync: Background synchronization every 60 seconds (configurable)
- Priority-Based: Operations are synced based on priority (CRITICAL > HIGH > NORMAL > LOW)
- Retry Logic: Automatic retry with exponential backoff (max 3 attempts)
- Thread-Safe: Supports concurrent operations safely
from whiteboxai import WhiteBoxAI
# Enable offline mode with auto-sync
client = WhiteBoxAI(
api_key="your-api-key",
enable_offline=True,
offline_dir="./whiteboxai_offline",
offline_auto_sync=True,
offline_sync_interval=60
)client = WhiteBoxAI(
api_key="your-api-key",
enable_offline=True, # Enable offline mode
offline_dir="./offline_queue", # Storage directory
offline_max_queue_size=10000, # Max queued operations (0 = unlimited)
)client = WhiteBoxAI(
api_key="your-api-key",
enable_offline=True,
offline_auto_sync=True, # Enable background sync
offline_sync_interval=60, # Sync every 60 seconds
)status = client.get_offline_status()
print(f"Queue size: {status['queue_size']}")
print(f"Last sync: {status['last_sync_time']}")
print(f"Failed operations: {status['failed_count']}")result = client.sync_offline_queue()
print(f"Synced: {result['synced']}")
print(f"Failed: {result['failed']}")# Remove operations older than 7 days
client.cleanup_offline_queue(older_than_days=7)Set priority for operations:
# HIGH priority prediction
monitor.log_prediction(
inputs={"feature1": 1.0},
output={"prediction": 0.85},
priority="HIGH"
)
# CRITICAL priority for important events
monitor.log_prediction(
inputs={"feature1": 1.0},
output={"prediction": 0.95},
priority="CRITICAL"
)Priority levels: CRITICAL, HIGH, NORMAL (default), LOW
- Storage Location: Choose a persistent location for
offline_dir - Queue Size: Set appropriate
offline_max_queue_sizebased on your storage - Sync Interval: Balance between freshness and API load
- Cleanup: Regularly cleanup old operations to prevent queue bloat
- Monitoring: Monitor queue size and failed operations
Offline mode handles errors gracefully:
try:
monitor.log_prediction(inputs=data, output=prediction)
except Exception as e:
# Operation is automatically queued if offline mode is enabled
print(f"Logged to offline queue: {e}")For production deployments:
client = WhiteBoxAI(
api_key=os.getenv("WHITEBOXAI_API_KEY"),
enable_offline=True,
offline_dir="/var/lib/whiteboxai/queue",
offline_max_queue_size=50000,
offline_auto_sync=True,
offline_sync_interval=30, # More frequent sync in production
)
# Monitor queue health
import schedule
def check_queue_health():
status = client.get_offline_status()
if status['queue_size'] > 10000:
alert_ops_team(f"Queue size: {status['queue_size']}")
schedule.every(5).minutes.do(check_queue_health)