Skip to main content
SGLang’s frontend language supports multiple backend providers, allowing you to use the same code with local models, hosted services, or cloud APIs. This page covers how to configure and use different backends.

Setting the Default Backend

Before executing SGLang functions, you must set a default backend:
You can also override the backend for individual calls:

Local Runtime

sgl.Runtime - Local Model Server

Run models locally using SGLang’s high-performance runtime:
Parameters:
  • model_path (str): HuggingFace model path or local path to model
  • tokenizer_path (str): Path to tokenizer (defaults to model_path)
  • port (int): Port for the HTTP server (auto-allocated if not specified)
  • host (str): Host address (default: “127.0.0.1”)
  • tp_size (int): Tensor parallelism size for multi-GPU
  • log_level (str): Logging level (“error”, “warning”, “info”, “debug”)
  • launch_timeout (float): Timeout for server startup (default: 300s)
  • Additional parameters from ServerArgs (see server documentation)
Example with Tensor Parallelism:
Example with Custom Chat Template:

sgl.RuntimeEndpoint - Connect to Running Server

Connect to an already-running SGLang server:
Parameters:
  • base_url (str): URL of the running SGLang server
  • api_key (Optional[str]): API key for authentication
  • verify (Optional[str]): SSL verification (path to cert or False)
  • chat_template_name (Optional[str]): Override chat template
Example with API Key:

Starting a Server Separately

You can also start the server via command line:
Then connect with RuntimeEndpoint:

OpenAI

sgl.OpenAI - OpenAI API

Use OpenAI models:
Parameters:
  • model_name (str): OpenAI model name
  • is_chat_model (Optional[bool]): Whether this is a chat model (auto-detected)
  • chat_template (Optional[ChatTemplate]): Custom chat template
  • api_key (str): API key (defaults to OPENAI_API_KEY env var)
  • base_url (str): Custom base URL for API
  • Other parameters passed to openai.OpenAI()
Example with Custom Parameters:
Using Different Models:
Vision Models:
O1 Models:

Azure OpenAI

Azure Configuration

Use Azure OpenAI Service:

Anthropic

sgl.Anthropic - Claude Models

Use Anthropic’s Claude models:
Parameters:
  • model_name (str): Claude model name
  • api_key (str): API key (defaults to ANTHROPIC_API_KEY env var)
  • Other parameters passed to anthropic.Anthropic()
Example:
Note: Anthropic automatically handles system messages from the messages array.

Other Cloud Providers

Google Vertex AI

Use Google’s Gemini models via Vertex AI:
Vision Models:

LiteLLM (Multiple Providers)

Use LiteLLM to access multiple providers with a unified interface:

Backend Utilities

Getting Server Information

Flushing Cache

Clear the KV cache on the server:

Profiling

For Runtime backends, enable profiling:

Complete Examples

Multi-Backend Function

Local Runtime with Multimodal Model

Batch Processing with Local Runtime

Together AI via LiteLLM

Backend Comparison

Best Practices

  1. Development vs Production: Use OpenAI or Anthropic for prototyping, Runtime for production
  2. Resource Management: Always call runtime.shutdown() when done with local runtimes
  3. Error Handling: Wrap backend initialization in try-except blocks
  4. API Keys: Use environment variables instead of hardcoding keys
  5. Timeout Configuration: Set appropriate timeouts for your use case
  6. Model Selection: Choose models based on task requirements (speed vs quality)
  7. Batch Processing: Use local Runtime for high-throughput batch jobs
  8. Testing: Test with multiple backends to ensure compatibility