Overview

The Perplexity SDKs provide extensive configuration options to customize client behavior for different environments and use cases. This guide covers retry configuration, timeout settings, and custom HTTP client setup.

Retries and Timeouts

Basic Retry Configuration

Configure how the SDK handles failed requests:
from perplexity import Perplexity
import httpx

client = Perplexity(
    max_retries=3,  # Default is 2
    timeout=httpx.Timeout(30.0, read=10.0, write=5.0, connect=2.0)
)

# Per-request configuration
search = client.with_options(max_retries=5).search.create(
    query="example"
)

Advanced Timeout Configuration

Set granular timeout controls for different phases of the request:
import httpx
from perplexity import Perplexity

# Detailed timeout configuration
timeout_config = httpx.Timeout(
    connect=5.0,    # Time to establish connection
    read=30.0,      # Time to read response
    write=10.0,     # Time to send request
    pool=10.0       # Time to get connection from pool
)

client = Perplexity(timeout=timeout_config)

# For long-running operations
long_timeout = httpx.Timeout(
    connect=5.0,
    read=120.0,  # 2 minutes for complex queries
    write=10.0,
    pool=10.0
)

client_long = Perplexity(timeout=long_timeout)

Custom HTTP Client

Proxy Configuration

Configure the SDK to work with corporate proxies:
import httpx
from perplexity import Perplexity, DefaultHttpxClient

# HTTP Proxy
client = Perplexity(
    http_client=DefaultHttpxClient(
        proxy="http://proxy.company.com:8080"
    )
)

# HTTPS Proxy with authentication
client_auth = Perplexity(
    http_client=DefaultHttpxClient(
        proxy="http://username:password@proxy.company.com:8080"
    )
)

# SOCKS proxy
client_socks = Perplexity(
    http_client=DefaultHttpxClient(
        proxy="socks5://proxy.company.com:1080"
    )
)

Custom Headers and User Agent

Add custom headers to all requests:
import httpx
from perplexity import Perplexity, DefaultHttpxClient

# Custom headers
headers = {
    "User-Agent": "MyApp/1.0",
    "X-Custom-Header": "custom-value"
}

client = Perplexity(
    http_client=DefaultHttpxClient(
        headers=headers
    )
)

# Advanced HTTP client configuration
transport = httpx.HTTPTransport(
    local_address="0.0.0.0",  # Bind to specific interface
    verify=True,              # SSL verification
    cert=None,                # Client certificate
    http2=True               # Enable HTTP/2
)

client_advanced = Perplexity(
    http_client=DefaultHttpxClient(
        transport=transport,
        headers=headers
    )
)

SSL/TLS Configuration

Configure SSL verification and certificates:
import httpx
import ssl
from perplexity import Perplexity, DefaultHttpxClient

# Disable SSL verification (not recommended for production)
client_no_ssl = Perplexity(
    http_client=DefaultHttpxClient(
        verify=False
    )
)

# Custom SSL context
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE

client_custom_ssl = Perplexity(
    http_client=DefaultHttpxClient(
        verify=ssl_context
    )
)

# Client certificate authentication
client_cert = Perplexity(
    http_client=DefaultHttpxClient(
        cert=("client.crt", "client.key")
    )
)

Connection Pooling

Optimize performance with connection pooling:
import httpx
from perplexity import Perplexity, DefaultHttpxClient

# Configure connection limits
limits = httpx.Limits(
    max_keepalive_connections=20,  # Keep-alive connections
    max_connections=100,           # Total connections
    keepalive_expiry=30.0         # Keep-alive timeout
)

client = Perplexity(
    http_client=DefaultHttpxClient(
        limits=limits
    )
)

# For high-throughput applications
high_throughput_limits = httpx.Limits(
    max_keepalive_connections=100,
    max_connections=500,
    keepalive_expiry=60.0
)

client_high_throughput = Perplexity(
    http_client=DefaultHttpxClient(
        limits=high_throughput_limits
    )
)

Environment-Specific Configuration

Development Configuration

Settings optimized for development and debugging:
import httpx
from perplexity import Perplexity, DefaultHttpxClient

# Development configuration
dev_client = Perplexity(
    max_retries=1,           # Fail fast in development
    timeout=httpx.Timeout(10.0),  # Short timeout
    http_client=DefaultHttpxClient(
        # Enable detailed logging
        event_hooks={
            'request': [lambda request: print(f"Request: {request.method} {request.url}")],
            'response': [lambda response: print(f"Response: {response.status_code}")]
        }
    )
)

Production Configuration

Settings optimized for production environments:
import httpx
from perplexity import Perplexity, DefaultHttpxClient

# Production configuration
prod_limits = httpx.Limits(
    max_keepalive_connections=50,
    max_connections=200,
    keepalive_expiry=60.0
)

prod_timeout = httpx.Timeout(
    connect=5.0,
    read=60.0,
    write=10.0,
    pool=10.0
)

prod_client = Perplexity(
    max_retries=3,
    timeout=prod_timeout,
    http_client=DefaultHttpxClient(
        limits=prod_limits,
        verify=True,  # Always verify SSL in production
        http2=True    # Enable HTTP/2 for better performance
    )
)

Configuration Patterns

Environment-Based Configuration

Use environment variables to configure the client:
import os
import httpx
from perplexity import Perplexity, DefaultHttpxClient

def create_client():
    # Base configuration
    timeout = httpx.Timeout(
        connect=float(os.getenv('PERPLEXITY_CONNECT_TIMEOUT', '5.0')),
        read=float(os.getenv('PERPLEXITY_READ_TIMEOUT', '30.0')),
        write=float(os.getenv('PERPLEXITY_WRITE_TIMEOUT', '10.0'))
    )
    
    max_retries = int(os.getenv('PERPLEXITY_MAX_RETRIES', '3'))
    
    # Optional proxy configuration
    proxy = os.getenv('PERPLEXITY_PROXY')
    http_client_kwargs = {}
    if proxy:
        http_client_kwargs['proxy'] = proxy
    
    return Perplexity(
        max_retries=max_retries,
        timeout=timeout,
        http_client=DefaultHttpxClient(**http_client_kwargs)
    )

client = create_client()

Configuration Factory

Create reusable configuration patterns:
import httpx
from perplexity import Perplexity, DefaultHttpxClient

class PerplexityClientFactory:
    @staticmethod
    def development():
        return Perplexity(
            max_retries=1,
            timeout=httpx.Timeout(10.0)
        )
    
    @staticmethod
    def production():
        return Perplexity(
            max_retries=3,
            timeout=httpx.Timeout(connect=5.0, read=60.0, write=10.0),
            http_client=DefaultHttpxClient(
                limits=httpx.Limits(
                    max_keepalive_connections=50,
                    max_connections=200
                )
            )
        )
    
    @staticmethod
    def high_throughput():
        return Perplexity(
            max_retries=2,
            timeout=httpx.Timeout(connect=2.0, read=30.0, write=5.0),
            http_client=DefaultHttpxClient(
                limits=httpx.Limits(
                    max_keepalive_connections=100,
                    max_connections=500
                )
            )
        )

# Usage
client = PerplexityClientFactory.production()