Skip to main content

Overview

DeploymentSampler handles client-side tokenization via a HuggingFace tokenizer and returns structured SampledCompletion objects with token IDs, logprobs, and completion metadata. Serverless and dedicated Training API sampling clients both use this implementation after their infrastructure-specific setup. Use it in training scripts that need token-level outputs (e.g. GRPO, DPO).

Constructor

The adaptive-by-default controller behavior and TITOSidecar API described on this page require fireworks-ai[training]>=1.2.11.

Concurrency Control

sample_with_tokens(n=K) fans out into K individual streaming requests. The sampler uses adaptive concurrency by default, so excess requests wait for a client-side slot instead of all starting at once. Two controllers are available: Auto-tunes the concurrency window using AIMD (Additive Increase / Multiplicative Decrease) from the congestion signals available on the target:
For dedicated deployments, the controller reads prefill_queue_duration from server response metrics. When that metric is unavailable, as on serverless, it uses HTTP 429/503 and transport failures as congestion signals instead. By default, it adjusts after every 32 completed requests and at step boundaries. Set adjustment_interval=0 to adjust only at step boundaries.

FixedConcurrencyController

Static semaphore — use when you know the right concurrency for your deployment:

sample_with_tokens(...)

Sample completions and return structured results with token IDs. This method is async, so call it with await or wrap it with asyncio.run(...) from synchronous code:

Retrieving inference logprobs

For GRPO importance sampling, pass logprobs=True:

Sequence length filtering

sample_with_tokens supports max_seq_len for automatic filtering:
Two levels of filtering are applied:
  1. Prompt pre-filter: If the tokenized prompt already meets or exceeds max_seq_len, the method returns an empty list immediately — no inference call is made.
  2. Completion post-filter: After sampling, any completion whose full token sequence (prompt + completion) exceeds max_seq_len is silently dropped.

sample_with_prompt_tokens(...)

Use sample_with_prompt_tokens when your renderer has already produced prompt token IDs. Both serverless and dedicated services expose the shared DeploymentSampler through the sampling client:
Keep sampling_client alive until all calls through sampler have finished, then close it to release the underlying HTTP clients.

RL rollout sampling

Use Inference for RL rollouts as the canonical reference for session affinity, session-ID lifecycle, and KV-cache behavior. The example below only shows how to pass a rollout session value through DeploymentSampler and fan out independent samples. The following example launches two independent trajectories for every pre-tokenized prompt:
Here, n=1 is intentional because sample_with_prompt_tokens(n=2, user=...) gives both child requests the same user value. With 32 entries in batch_prompt_token_ids, the example creates 64 individual sampling requests. Follow Inference for RL rollouts to decide when those requests should use distinct or shared session values.

TITO sidecars for agent harnesses

TITOSidecar is the SDK’s environment-local adapter for exact-token, multi-turn RL. Construct it from a DeploymentSampler and a certified conversation renderer, then give the returned trajectory endpoint to an OpenAI-compatible agent harness:
The sidecar listens only on loopback inside the agent’s Docker container or remote sandbox. It translates chat messages and tools into exact prompt token IDs, samples through the borrowed DeploymentSampler, and records aligned completion IDs, log probabilities, and optional Router Replay data. The default is full-history rendering with explicit append, bounded realign, or new-segment decisions; experimental incremental rendering requires a separately certified renderer contract. The SDK surface is harness-neutral and does not provide model-specific renderers or environment lifecycle. Most users should start from the Cookbook Harbor integrations, which supply the renderer, Pi/OpenCode/Mini-SWE-Agent adapters, Docker/E2B lifecycle, retry policy, and conversion from the compact TITO artifact to RolloutRun.

SampledCompletion

Each completion returned by sample_with_tokens or sample_with_prompt_tokens: