Send multiple traces in a single call using send_tasks_batch().

Basic Usage

from infinium import InfiniumClient, LlmUsage

client = InfiniumClient(agent_id="...", agent_secret="...")

tasks = []
for doc in documents:
    start = time.time()
    resp = openai.chat.completions.create(
        model="gpt-4o",
        messages=[
            {"role": "system", "content": "Summarize this document in 2 sentences."},
            {"role": "user", "content": doc["content"]},
        ],
    )
    duration = time.time() - start

    tasks.append(InfiniumClient.create_task_data(
        name=f"Summarize: {doc['title'][:60]}",
        description=f"2-sentence summary of '{doc['title']}'",
        duration=duration,
        input_summary=f"Document: {doc['title']}",
        output_summary=resp.choices[0].message.content,
        llm_usage=LlmUsage(
            model="gpt-4o", provider="openai",
            prompt_tokens=resp.usage.prompt_tokens,
            completion_tokens=resp.usage.completion_tokens,
            total_tokens=resp.usage.total_tokens,
            api_calls_count=1,
        ),
    ))

result = client.send_tasks_batch(tasks)
print(f"Sent {result.successful}/{result.successful + result.failed} traces")

Parameters

Sync: send_tasks_batch()

ParameterTypeDefaultDescription
taskslist[TaskData]requiredList of TaskData objects to send
max_concurrentint5Maximum number of tasks sent per chunk

Async: send_tasks_batch()

ParameterTypeDefaultDescription
taskslist[TaskData]requiredList of TaskData objects to send
max_concurrentint5Maximum number of concurrent sends
fail_fastboolFalseStop on first error (async only)

BatchResult

The return value is a BatchResult dataclass:

FieldTypeDescription
successfulintNumber of traces sent successfully
failedintNumber of traces that failed
resultslist[ApiResponse]Individual response for each task
errorslist[str]Error messages for failed sends

Async Batch with fail_fast

result = await client.send_tasks_batch(
    tasks,
    max_concurrent=10,
    fail_fast=True,  # Stop immediately on first failure
)

if result.failed > 0:
    print(f"Failed after {result.successful} successes:")
    for error in result.errors:
        print(f"  - {error}")

Error Handling

Individual task failures don’t stop the batch (unless fail_fast=True in async). Check the BatchResult to see which tasks succeeded or failed:

result = client.send_tasks_batch(tasks)

for i, resp in enumerate(result.results):
    if not resp.success:
        print(f"Task {i} failed: {resp.message}")

For catastrophic batch failures, a BatchError exception is raised:

from infinium.exceptions import BatchError

try:
    result = client.send_tasks_batch(tasks)
except BatchError as e:
    print(f"Batch failed: {e.successful_count} OK, {e.failed_count} failed")
    for error in e.errors:
        print(f"  - {error}")