SGLang is an open-source serving framework for large language models. The SGLang OpenAI-compatible API lets you send chat requests to a self-hosted model with curl or the OpenAI Python client. If you run SGLang behind Hivenet Router, your application sends POST /v1/chat/completions to Router. A Router agent connects that endpoint to the model on your SGLang server.
This guide is for developers connecting an existing SGLang chat model to an application. You’ll look up its model ID, register it with Router, and use the same ID for your first request. Checking the ID at both endpoints helps you distinguish a backend problem from a Router registration or access problem.
This walkthrough assumes one backend and one chat model. If you’re still choosing an inference engine, the SGLang vs vLLM comparison covers model compatibility, caching, and how to compare performance on your own workload.
Start with a working SGLang server, a running Hivenet Router, and a compatible hivenet-agent binary on the backend host. The examples use Bash, jq, and curl 7.76 or later for the --fail-with-body option. They assume that Router requires an inference key. Have the Router’s gRPC address ready for the agent and its HTTP origin ready for the client. The agent host also needs network access to the Router’s configured peer port. The SGLang agent deployment guide includes the Docker setup and example ports.
You’ll use the Router’s shared JWT secret file to register the agent and a Router inference key to make the client request. If SGLang itself requires an API key, have that backend key available as well. Keep the shared secret on the agent host.
On the agent host, set the backend address to the host and port where SGLang is listening. The example uses port 3000. If the agent runs in a container, use an address reachable from inside that container. A loopback address points back to the container unless it shares the host network.
Wait for HTTP 200 from the health check. Copy the full ID for the chat model you intend to serve, including any organization prefix. A Hugging Face repository name or local model path may differ from this ID. For example, if the server starts with --served-model-name support-chat, look for support-chat in the model list. SGLang documents this override in its server arguments reference. Set MODEL_ID to that exact string in the shell you’ll use for the agent and again in the client shell.
The agent represents one model. When you omit --model, the agent uses the first ID SGLang returns. Setting it explicitly makes the registration reproducible if the backend lists several IDs. To expose a second model, run another agent with its own identity file and the second model’s ID.
In the agent shell, set BACKEND_URL to the address you just checked. Replace the sample gRPC address and file paths below with your deployment values. The secret file must match the Router’s secret, and the agent needs write access to the directory containing its persistent identity file. If SGLang requires a key, also supply --backend-api-key-file with the path to a file containing that key.
Leave the agent running. For a chat model, the documented readiness check includes a one-token chat completion before registration. A healthy SGLang process can therefore appear in the local check before the model appears at Router.
Open a second Bash shell for the client and leave the agent running in the first. Replace both example values below. Use the model ID you copied from SGLang and the client-facing Router origin without a trailing slash or /v1. Load the inference key into ROUTER_CLIENT_KEY from your usual credential store. The model lookup prints the exact ID if the key can access it.
Once the ID appears, send a short, nonstreaming request. The model field selects the served model, and messages holds the conversation. For the first turn, send a single user message with the prompt in content. Using jq to build the JSON preserves the model string you just checked. A successful response contains the assistant’s reply in choices[0].message.content. For example, an answer of “Mars” appears inside the first choice’s message object. Keep the full JSON while troubleshooting so you can inspect the response format before extracting the text.
This checks the path your application uses. If several agents serve the same model, the reply alone cannot identify which replica answered. An operator can inspect /admin/routing-table on Router to check the registered agent’s engine and model. For the underlying chat request and response format, see SGLang’s OpenAI-compatible API reference.
Once curl returns an answer, you can reuse the model ID and inference key in your application. For the Python client, the base URL ends in /v1. The client adds /chat/completions when you call chat.completions.create. Point the base URL at Router so that the request follows the route you just checked.
If your application already uses the OpenAI Python SDK, reuse the installed client. Otherwise, use pip to install the openai package in the application’s Python environment. Run this example from the same client shell so Python inherits the three variables you just exported.
For a text chat that already uses the OpenAI Python SDK, keep the messages list and the chat.completions.create call. Change the base URL, API key, and model name. For a local deployment, the inference request then goes to your own Router and SGLang backend. If the application expects structured output, test its JSON schema with the selected backend. For tool calling, use a model with function-calling support and check that the response contains the tool name and arguments your application expects. An HTTP 200 from the plain-text example only checks the basic chat path. See SGLang’s structured output formats.
The curl example requests one JSON response so you can inspect the complete message. If your application displays text as it arrives, set stream to true. The chat endpoint then streams server-sent events (SSE). Read text from choices[0].delta.content when present and append it in arrival order. With curl, add --no-buffer to see the chunks as they arrive.
For a conversational reply, send the message list to Router at /v1/chat/completions. Some SGLang examples call the server’s native generation endpoint at /generate directly. That path uses a different request format. Keep the chat endpoint when moving the example into an OpenAI client.
Choose the endpoint and model together. For example, SGLang’s embeddings API takes input at /v1/embeddings and returns a vector. To ask a question about an image, use a vision-language model and include an image in the chat message as shown in SGLang’s vision example. Those examples describe SGLang’s backend capabilities. The Router request in this guide verifies only text chat with the model you registered.
Check the backend key on the direct request and the key file supplied to the agent. The agent uses its own backend credential when it calls SGLang.
Check the agent logs for warm-up or registration errors. Verify the gRPC address and shared secret, then inspect the routing table. If the agent is registered, check which models the client key can access.
Read the HTTP status and response body. Send the same JSON directly to SGLang’s /v1/chat/completions from the agent host, using the backend credential if required. If the direct request also fails, start with the backend response and SGLang logs. If the direct request succeeds, check the Router and agent logs for the routed request.
Start SGLang with --enable-metrics to expose engine signals such as queued requests and time to first token. For the next checks, see monitoring and troubleshooting Hivenet Router.
Pick one AI, compute, or storage workload and see the difference for yourself. Spin it up in minutes, or let our team map your fastest path to production.