Skip to main content

TokenBatcher

Token-aware batcher for LLM inference workloads.

Constructor​

Signature​

def TokenBatcher(
self,
inference_fn: ProcessFn[RecordT, ResultT] | None = None,
*,
process_fn: ProcessFn[RecordT, ResultT] | None = None,
token_estimator: CostEstimatorFn[RecordT] | None = None,
cost_estimator: CostEstimatorFn[RecordT] | None = None,
target_batch_tokens: int | None = None,
target_batch_cost: int = 32_000,
default_token_estimate: int | None = None,
default_cost: int = 1,
max_batch_size: int = 256,
min_batch_size: int = 1,
batch_timeout_s: float = 0.05,
max_queue_size: int = 5_000,
prefetch_batches: int = 2,
): ...

Parameters​

NameTypeDescription
inference_fn`ProcessFn[RecordT, ResultT]None` = None
process_fn`ProcessFn[RecordT, ResultT]None` = None
token_estimator`CostEstimatorFn[RecordT]None` = None
cost_estimator`CostEstimatorFn[RecordT]None` = None
target_batch_tokens`intNone` = None
target_batch_costint = 32000The target total cost per batch.
default_token_estimate`intNone` = None
default_costint = 1Default cost if estimation fails.
max_batch_sizeint = 256Maximum number of records in a single batch.
min_batch_sizeint = 1Minimum number of records required to trigger processing before timeout.
batch_timeout_sfloat = 0.05Time to wait in seconds before processing a partial batch.
max_queue_sizeint = 5000Maximum number of records allowed in the queue.
prefetch_batchesint = 2Number of batches to prefetch for processing.

Methods​


submit()​

async def submit(
self,
record: RecordT,
*,
estimated_tokens: int | None = None,
estimated_cost: int | None = None,
) -> asyncio.Future[ResultT]: ...

Submit a single record for batched inference. Accepts either estimated_tokens or estimated_cost.

Parameters​

NameTypeDescription
recordRecordTThe input record.
estimated_tokens`intNone`
estimated_cost`intNone`

Returns​

TypeDescription
asyncio.Future[ResultT]A future whose result is the corresponding entry from the list returned by the inference function.