> ## Documentation Index
> Fetch the complete documentation index at: https://docs.levanto.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI SDK

> Sage decisions through the OpenAI SDK's Decisions API, in Python and Node.

```bash Python theme={"theme":"github-light"}
pip install openai
```

```bash TypeScript theme={"theme":"github-light"}
npm install openai
```

Point the client at Levanto. The same client works for [Levanto's LLMs](https://docs.levanto.ai/llms/introduction.md).

## Decisions

```python Python theme={"theme":"github-light"}
import os

from openai import OpenAI

client = OpenAI(base_url="https://api.levanto.ai/v1", api_key=os.environ["LEVANTO_API_KEY"])

decision = client.decisions.create(
    model="sage-latest",
    input="I was charged twice for my order. Please refund the duplicate today.",
    questions=[
        {"type": "predicate", "name": "refund", "instructions": "The customer asks for money back."},
        {
            "type": "choice",
            "name": "team",
            "instructions": "Which team should handle this?",
            "choices": [{"value": "billing"}, {"value": "technical"}, {"value": "shipping"}],
        },
        {
            "type": "score",
            "name": "urgency",
            "instructions": "How urgent is it?",
            "levels": [{"label": "Can wait"}, {"label": "This week"}, {"label": "Today"}],
        },
    ],
)

refund, team, urgency = decision.answers
refund.probability  # 0.95
team.choice         # "billing"
urgency.score       # 1.99, from 0 to 2
```

```ts TypeScript theme={"theme":"github-light"}
import OpenAI from 'openai';

const client = new OpenAI({ baseURL: 'https://api.levanto.ai/v1', apiKey: process.env.LEVANTO_API_KEY });

const decision = await client.decisions.create({
  model: 'sage-latest',
  input: 'I was charged twice for my order. Please refund the duplicate today.',
  questions: [
    { type: 'predicate', name: 'refund', instructions: 'The customer asks for money back.' },
    {
      type: 'choice',
      name: 'team',
      instructions: 'Which team should handle this?',
      choices: [{ value: 'billing' }, { value: 'technical' }, { value: 'shipping' }],
    },
    {
      type: 'score',
      name: 'urgency',
      instructions: 'How urgent is it?',
      levels: [{ label: 'Can wait' }, { label: 'This week' }, { label: 'Today' }],
    },
  ],
});

for (const answer of decision.answers) {
  if (answer.type === 'predicate') answer.probability; // refund: 0.95
  if (answer.type === 'choice') answer.choice;         // team: 'billing'
  if (answer.type === 'score') answer.score;           // urgency: 1.99, from 0 to 2
}
```

Answers come back in the order of `questions`, and every question gets one: set your own threshold on the probability. To get `null` when Sage isn't sure, or to use Sort, Tags or reasoning, call [`/decide`](https://docs.levanto.ai/decision-model/quickstart). Request and answer fields: [OpenAI Decisions API](https://docs.levanto.ai/openai-decisions.md).

## Images

```python Python theme={"theme":"github-light"}
import base64

with open("screenshot.png", "rb") as f:
    image = "data:image/png;base64," + base64.b64encode(f.read()).decode()

decision = client.decisions.create(
    model="sage-latest",
    input=[{
        "role": "user",
        "content": [
            {"type": "input_text", "text": "Screenshot attached to a support ticket."},
            {"type": "input_image", "image_url": image},
        ],
    }],
    questions=[{"type": "predicate", "name": "payment_error", "instructions": "The screenshot shows a payment error."}],
)
decision.answers[0].probability  # 0.97
```

```ts TypeScript theme={"theme":"github-light"}
import { readFileSync } from 'node:fs';

const image = 'data:image/png;base64,' + readFileSync('screenshot.png').toString('base64');

const decision = await client.decisions.create({
  model: 'sage-latest',
  input: [{
    role: 'user',
    content: [
      { type: 'input_text', text: 'Screenshot attached to a support ticket.' },
      { type: 'input_image', image_url: image },
    ],
  }],
  questions: [{ type: 'predicate', name: 'payment_error', instructions: 'The screenshot shows a payment error.' }],
});
```

One image per request, as a `data:` URI.

## Tool calls

`input` can be the agent's conversation, with its `function_call` and `function_call_output` items, so Sage judges a pending call against what the user asked. See [Guard tool calls](https://docs.levanto.ai/decision-model/tool-calls.md).

## Check it worked

`decision.model` is `levanto-sage-2`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.