翻篇收录说明
翻篇网页快照

Payload Optimization - Cerebras Inference

https://inference-docs.cerebras.ai/capabilities/payload-optimization

最近抓取:2026-12-04 03:12:50(北京时间)

最近内容变化:2026-12-01 00:28:30(北京时间)

这是最近一次有效抓取的文本快照,不含网页脚本、图片和动态交互内容。实际网页可能已更新。

访问原网页

页面简介

Reduce latency by compressing request payloads with msgpack encoding and gzip.

正文摘录

For requests with large payloads, you can reduce the size of the request body sent to the Cerebras API by using msgpack encoding, gzip compression, or both. This can meaningfully improve time-to-first-token (TTFT) for requests with long prompts. Payload optimization is supported on /v1/chat/completions and /v1/completions. Support with Dedicated Inference may vary by model. Encoding Options The Cerebras API accepts the following request body encodings. The table shows the expected payload size reduction for each option: Content-TypeDescriptionChat Completions1Completions2application/jsonDefault JSON encodingBaselineBaselineapplication/vnd.msgpackmsgpack binary encodingup to ~5%up to ~56%application/json + Content-Encoding: gzipJSON with gzip compressionup to ~98%up to ~68%application/vnd.msgpack + Content-Encoding: gzipmsgpack + gzipup to ~98%up to ~69% 1 Measured against a 50k-token chat completions payload (206 KB JSON baseline). 2 Measured against a 50k token-ID completions payload (331 KB JSON baseline). You can use msgpack encoding or gzip compression independently, or combine them for maximum compression. Smaller request payloads reduce network transfer time, a contributing factor to TTFT. Actual TTFT improvement will vary, as network transfer is one of several factors that contribute to overall latency. When to Use Payload Optimization Optimizing payload size with request compression is most beneficial for: Long prompts – requests with long system prompts, extensive conversation history, or large code blocks. Gzip compression is the most effective option for these payloads. Token-ID completions – /v1/completions payloads using integer token arrays benefit from both msgpack encoding and gzip compression Tool call-heavy payloads – requests with many tool definitions or deeply nested JSON structures. Both msgpack encoding and gzip compression provide savings. For small requests (under a few KB), the overhead of compression may outweigh the savings. Standard JSON encoding is fine for typical chat interactions. Size reductions depend on payload content. The benchmarks above used a token-ID completions payload, where msgpack’s integer encoding provides the greatest benefit. String-heavy chat payloads may see smaller msgpack reductions, while payloads with deeply nested structures (e.g., tool calls) may see greater savings. Gzip benefits are more consistent across payload types. msgpack Encoding msgpack is a binary serialization format that produces smaller payloads than equivalent JSON. To use it, serialize your request body with msgpack and set the Content-Type header to application/vnd.msgpack. PythonNode.jscURLimport msgpack import requests import os url = "https://api.cerebras.ai/v1/chat/completions" api_key = os.environ.get("CEREBRAS_API_KEY") payload = { "model": "qwen-3.8-27b", "messages": [ {"role": "user", "content": "Explain quantum computing in one paragraph."} ] } response = requests.post( url, data=msgpack.packb(payload), headers={ "Content-Type": "application/vnd.msgpack", "Authorization": f"Bearer {api_key}" } ) print(response.json()["choices"][0]["message"]["content"]) Gzip Compression You can gzip-compress any request body and set the Content-Encoding: gzip header. This works with both JSON and msgpack payloads. PythonNode.jscURLimport gzip import json import requests import os url = "https://api.cerebras.ai/v1/chat/completions" api_key = os.environ.get("CEREBRAS_API_KEY") payload = { "model": "qwen-3.8-27b", "messages": [ {"role": "user", "content": "Explain quantum computing in one paragraph."} ] } json_bytes = json.dumps(payload).encode("utf-8") compressed = gzip.compress(json_bytes, compresslevel=5) response = requests.post( url, data=compressed, headers={ "Content-Type": "application/json", "Content-Encoding": "gzip", "Authorization": f"Bearer {api_key}" } ) print(response.json()["choices"][0]["message"]["content"]) Combining Both For maximum compression, use msgpack encoding with gzip compression: PythonNode.jscURLimport gzip import msgpack import requests import os url = "https://api.cerebras.ai/v1/chat/completions" api_key = os.environ.get("CEREBRAS_API_KEY") payload = { "model": "qwen-3.8-27b", "messages": [ {"role": "user", "content": "Explain quantum computing in one paragraph."} ] } data = msgpack.packb(payload) compressed = gzip.compress(data, compresslevel=5) response = requests.post( url, data=compressed, headers={ "Content-Type": "application/vnd.msgpack", "Content-Encoding": "gzip", "Authorization": f"Bearer {api_key}" } ) print(response.json()["choices"][0]["message"]["content"]) Was this page helpful?YesNo⌘ICopy pageCopy MCP ServerView as Markdown