Skip to main content

Overview

LiteLLM supports async operations for all API calls, allowing you to make concurrent requests and improve throughput in async applications.

Basic Usage

Async Functions

All LiteLLM functions have async equivalents prefixed with a:
  • completion()acompletion()
  • embedding()aembedding()
  • image_generation()aimage_generation()
  • transcription()atranscription()
  • speech()aspeech()

Function Signature

Examples

Single Async Request

Concurrent Requests

Multiple Models Concurrently

Error Handling with Async

Async with Retries

Async Streaming

Basic Async Streaming

Multiple Concurrent Streams

Async with Other Operations

Async Embeddings

Async Image Generation

Performance Optimization

Semaphore for Rate Limiting

Timeout and Cancellation

Batch Processing with Chunks

Integration Examples

With FastAPI

With asyncio and aiohttp

Best Practices

  1. Use connection pooling: Reuse HTTP connections for better performance
  2. Set appropriate timeouts: Prevent hanging requests with timeout parameters
  3. Implement rate limiting: Use semaphores to control concurrent requests
  4. Handle errors gracefully: Always wrap async calls in try-except blocks
  5. Use asyncio.gather(): For concurrent operations with no dependencies
  6. Monitor resource usage: Async doesn’t mean unlimited concurrency

Troubleshooting

Common Issues

RuntimeError: This event loop is already running
Memory Issues with Many Concurrent Requests
Timeout Errors