TGI API
The TGI API exposes an OpenAI-compatible HTTP surface. Point any OpenAI SDK, LangChain adapter, or custom client at the TGI server base URL — no provider-specific rewrites.
Base URL
| Deployment | Base URL |
|---|---|
Local Docker (-p 8080:80) | http://localhost:8080/v1 |
| K8s ingress | https://tgi.your-domain.com/v1 |
| HF Inference Endpoints | Provided in the endpoint dashboard |
The API key can be any non-empty string when TGI runs without auth — set a real key at your ingress layer in production.
Chat completions
curl http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer tgi" \
-d '{
"model": "meta-llama/Llama-3.1-8B-Instruct",
"messages": [{"role": "user", "content": "Classify intent: refund request"}],
"max_tokens": 64,
"temperature": 0
}'Request fields
| Field | Required | Notes |
|---|---|---|
model | Yes | Must match the --model-id loaded by the server |
messages | Yes | Standard OpenAI chat message array |
max_tokens | Yes | Cap output length — enforce in every client |
temperature | No | Lower for classification; higher for creative tasks |
stream | No | Set true for SSE token streaming |
Streaming
const stream = await client.chat.completions.create({
model: "meta-llama/Llama-3.1-8B-Instruct",
messages: [{ role: "user", content: "Draft a one-line status update." }],
max_tokens: 128,
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}Streaming reduces time-to-first-token for chat UIs. Attribute spans with inference.stream=true for latency dashboards.
OpenAI SDK
import OpenAI from "openai";
const client = new OpenAI({
baseURL: process.env.TGI_BASE_URL ?? "http://localhost:8080/v1",
apiKey: process.env.TGI_API_KEY ?? "tgi",
});
const response = await client.chat.completions.create({
model: "meta-llama/Llama-3.1-8B-Instruct",
messages: [{ role: "user", content: "Extract entities from: ACME Corp, Q3 renewal" }],
max_tokens: 128,
});Works with Python (openai package), Node, and any HTTP client that speaks the OpenAI schema.
Server-enforced limits
TGI rejects requests that exceed server flags regardless of client max_tokens:
| Server flag | API effect |
|---|---|
--max-input-length | 4xx when prompt tokens exceed cap |
--max-total-tokens | 4xx when input + requested output exceeds cap |
Configure limits on the server and validate in CI that agents always send max_tokens.
Health and info endpoints
| Endpoint | Purpose |
|---|---|
GET /health | Liveness — returns 200 when the server is ready |
GET /info | Model metadata, version, and loaded weights info |
Use /health for K8s probes. See Kubernetes for probe configuration.
Version differences
Older TGI builds may expose /generate instead of /v1/chat/completions. Pin your container image tag in production and test the path your client expects.
| Symptom | Fix |
|---|---|
404 on /v1/chat/completions | Upgrade TGI or switch to the legacy generate endpoint |
| 422 on large prompts | Lower prompt size or raise --max-input-length |
| Empty stream chunks | Check stream_options compatibility with your SDK version |
Instrumentation
Tag every API call with span labels for cost attribution:
// Pseudocode — wrap your OpenAI client middleware
span.setAttribute("inference.backend", "tgi");
span.setAttribute("model_id", "meta-llama/Llama-3.1-8B-Instruct");
span.setAttribute("max_tokens", 64);See Observability for Prometheus metrics on the server side.
Related
- Server — start the TGI server and configure model flags
- Guardrails — enforce output caps at ingress and CI
- TGI API docs