Skip to main content
The official Python SDK for the Krun API.

Install

Set your API key:

Basic decision

decide() returns a DecisionResult: Each ChoiceAnswer has type, choice (str | None), confidence, probabilities, abstain and abstention_status. See Questions and answers.
When the model abstains, choice is None. The SDK never replaces it with the most likely option. Read probabilities for a best guess. See Abstention.
decide() arguments:

Multiple questions

Ask several questions about the same context in one call:
See Multiple questions.

Tool routing

Set task_type="tool" on the question:
Questions can also be written as objects with ChoiceQuestion:
See Tool routing.

Feedback

feedback() returns a Feedback object with id, request_id, question_id and created_at. It raises NotFoundError if the request id was not decided by your project. See Feedback.

Models

Async client

AsyncKrun has the same arguments, methods and return types as Krun, as coroutines:
Concurrent requests count toward your per-minute rate limit. To ask several questions about the same text, use one request with multiple questions instead.

Configuration

Use the client as a context manager, or call client.close(), to release connections:
If no API key is passed and KRUN_API_KEY is not set, Krun() raises KrunError. The key never appears in repr(client), error messages or logs.

Timeouts

The default timeout is 70 seconds per attempt, because a serverless cold start can use most of the API’s 60-second deadline. The timeout can’t be disabled: None, 0 and infinity are rejected. When it elapses, the SDK raises APITimeoutError.
See Timeouts and cold starts.

Retries

The API already retries its model backend, so the SDK retries only a little:
  • The wait follows Retry-After when the API sends it, up to 10 seconds. Otherwise it is 0.5 s, then 1 s, 2 s, and so on, with jitter.
  • The SDK’s own timeout (APITimeoutError) is not retried.
  • Other 4xx errors and 500 are not retried.
  • A retried decide() can count one extra decision against usage if the first attempt reached the model.
  • Set max_retries=0 to disable retries.

Errors

All errors inherit from krun.KrunError and expose message, request_id, status_code and error_code when available.
Arguments of the wrong type, like context=None, raise TypeError before any request is sent. See Errors for the API error codes.

Logging

The SDK is silent by default and has no telemetry. It logs to the krun logger at DEBUG level: method, path, status, request id and retries only. It never logs the API key, context, options or answers.

Types

The krun package exports typed request and response types: ChoiceQuestion, ChoiceQuestionParam, QuestionsParam, DecisionResult, ChoiceAnswer, Usage, Feedback, Model, TaskType and AbstentionStatus. The package ships with py.typed, so type checkers like mypy and pyright use them.