## Deciding


## decide.decide()


Ask every question about `state` in one request, asynchronously; [decide_sync](deciding.md#spawnllm.decide.decide_sync) documents the contract.


Usage

``` python
decide.decide(
    state, questions, *, provider=JEV, timeout=DEFAULT_TIMEOUT, api_key=None
)
```


The call runs [decide_sync](deciding.md#spawnllm.decide.decide_sync) on a worker thread, so it shares the process's keep-alive client and holds no connection to the event loop.


#### Example

``` python
>>> decision = await decide({"title": "Revert the release"}, {
...     "kind": Label("What does the title ask for?", {"other": None, "rollback": None}),
... }, provider=OPENAI)
```


## decide.decide_sync()


Ask every question about `state` in one request and return the typed answers.


Usage

``` python
decide.decide_sync(
    state, questions, *, provider=JEV, timeout=DEFAULT_TIMEOUT, api_key=None
)
```


Keep each question to one observable fact and combine the answers in code; put counting, dates, and arithmetic in code too. Retries 408, 429, 5xx, 529, and a lost connection with backoff from 0.5s doubling to 5s, honoring `retry-after`, and only while the retry still fits inside `timeout`. The Keychain read counts against `timeout` too, and an answer that arrives after it raises `TimeoutError` rather than returning late.


#### Parameters


`state: str | Mapping[str, Any] | Sequence[Any]`  
The text to judge, or JSON data; Jev reads structure natively and OpenAI receives it as compact JSON text.

`questions: Mapping[str, Question]`  
Each question id mapped to its question, in the order to ask them.

`provider: `<a href="deciding.html#spawnllm.Provider" class="gdls-link gdls-code"><code>Provider</code></a>` = JEV`    
`JEV` or `OPENAI`, or a [Provider](deciding.md#spawnllm.Provider) pinning another model.

`timeout: float = DEFAULT_TIMEOUT`    
Seconds for the whole call, retries included.

`api_key: str | None = None`  
The provider key; `None` reads it through [api_key](deciding.md#spawnllm.decide.api_key).


#### Returns


<a href="deciding.html#spawnllm.Decision" class="gdls-link gdls-code"><code>Decision</code></a>  
The decision, with a [Refused](deciding.md#spawnllm.Refused) answer for any question the provider declined.


#### Raises


`TimeoutError`  
When no answer arrives within `timeout`.

<a href="deciding.html#spawnllm.DecideError" class="gdls-link gdls-code"><code>DecideError</code></a>  
When the provider rejects the request.

<a href="deciding.html#spawnllm.DecideKeyMissing" class="gdls-link gdls-code"><code>DecideKeyMissing</code></a>  
When no key is passed or found.

`ValueError`  
When the question set breaks a limit both providers share, such as a label with fewer than 2 options or a score with more than 10 levels.


#### Example

``` python
>>> decision = decide_sync("Please roll back last night's release.", {
...     "rollback": Binary("Does the message ask to roll back a release?"),
... })
>>> decision.answers["rollback"].p_yes > 0.5
```

True


## Binary


A yes/no question; its answer is the probability of yes.


Usage

``` python
Binary(instructions, yes=None, no=None)
```


#### Attributes


`instructions: str`  
The question, about one observable fact.

`yes: str | None`  
What a yes means, when the question alone leaves it open.

`no: str | None`  
What a no means, when the question alone leaves it open.


#### Example

``` python
>>> Binary("Does the message ask to roll back a release?")
```


## Label


Pick one option from 2 to 255; put the fail-safe option first, since Jev leans toward it.


Usage

``` python
Label(instructions, options)
```


#### Attributes


`instructions: str`  
What to decide.

`options: Mapping[str, str | None]`  
Each option value mapped to its description, or `None`, in declared order.


#### Example

``` python
>>> Label("Which release action does the message ask for?", {"none": None, "rollback": "Undo a release"})
```


## Score


Rate along 2 to 10 ordered levels, lowest first; the score is the probability-weighted level index.


Usage

``` python
Score(instructions, levels)
```


#### Attributes


`instructions: str`  
What to rate.

`levels: Mapping[str, str | None]`  
Each level label mapped to its description, or `None`, lowest first.


#### Example

``` python
>>> Score("How urgent is the message?", {"Not urgent": None, "Somewhat urgent": None, "Blocking": None})
```


## Decision


One provider response: an answer per question id, in the order asked.


Usage

``` python
Decision(answers, model, input_tokens, latency_ms)
```


#### Attributes


`answers: Mapping[str, Answer]`  
Each question id mapped to its answer.

`model: str`  
The model that answered, as the provider reports it.

`input_tokens: int`  
Billed input tokens.

`latency_ms: float`  
Wall time of the attempt that answered, request to parsed response.


## BinaryAnswer


The answer to a [Binary](deciding.md#spawnllm.Binary) question.


Usage

``` python
BinaryAnswer(p_yes, confidence)
```


#### Attributes


`p_yes: float`  
Probability the answer is yes.

`confidence: float`  
`abs(2 * p_yes - 1)`, 0 at a coin flip and 1 at certainty.


## LabelAnswer


The answer to a [Label](deciding.md#spawnllm.Label) question.


Usage

``` python
LabelAnswer(choice, probabilities, confidence)
```


#### Attributes


`choice: str`  
The most likely option value.

`probabilities: Mapping[str, float]`  
Every option value mapped to its probability, in declared order.

`confidence: float`  
How far the distribution sits from uniform, 0 to 1.


## ScoreAnswer


The answer to a [Score](deciding.md#spawnllm.Score) question.


Usage

``` python
ScoreAnswer(score, probabilities, confidence)
```


#### Attributes


`score: float`  
The probability-weighted level index; it can land between levels.

`probabilities: Mapping[str, float]`  
Every level label mapped to its probability, lowest first.

`confidence: float`  
How concentrated the distribution is around its top level, 0 to 1.


## Refused


A question the provider declined to answer while it answered the others.


Usage

``` python
Refused()
```


## Provider


A decision provider and the model it runs.


Usage

``` python
Provider(name, model)
```


#### Attributes


`name: TDecideProvider`  
`jev` posts to TypeSafe's System One endpoint, `openai` to OpenAI's `/v1/decisions`.

`model: str`  
The literal model id sent with every request; pin a versioned id once a threshold is tuned against it.


#### Example

``` python
>>> Provider("jev", "jev-1.13.0")
```


## DecideError


The provider rejected a decision request, or answered one with a body that matches no question.


Usage

``` python
DecideError(status, message)
```


#### Attributes


`status`  
The HTTP status of the failed response.

`message`  
The provider's error body, or what about its answer failed to match.


## DecideKeyMissing


No API key for the provider: none was passed, its variable is unset, and the Keychain holds none.


Usage

``` python
DecideKeyMissing()
```


## decide.api_key()


Return the provider's API key from its environment variable, else from the macOS Keychain.


Usage

``` python
decide.api_key(provider, *, timeout=DEFAULT_TIMEOUT)
```


Jev reads `TYPESAFE_API_KEY` and OpenAI reads `OPENAI_API_KEY`; on macOS an unset variable falls through to the Keychain item [store_key](deciding.md#spawnllm.decide.store_key) wrote, read when the call is made.


#### Raises


<a href="deciding.html#spawnllm.DecideKeyMissing" class="gdls-link gdls-code"><code>DecideKeyMissing</code></a>  
When neither source holds a key.

`TimeoutError`  
When the Keychain read outlasts `timeout`.


## decide.store_key()


Store `key` as the provider's macOS Keychain item, replacing any earlier one.


Usage

``` python
decide.store_key(provider, key)
```


The key reaches `security` over stdin, never its argv, so no process listing shows it.


#### Raises


`ValueError`  
When the key holds characters outside `[A-Za-z0-9._-]`.

`subprocess.CalledProcessError`  
When `security` refuses the write.
