# Invoke LLM

`POST /compute/llms/{modelId}/invoke` · Core · Compute & Automation

Dispatches a synchronous inference request to the specified LLM model and returns `{ completion: string, tokensUsed: int, latencyMs: int }`. Requires `PROJECT_AUTHOR` or `PROJECT_ADMIN` as an LLM cost gate. Returns `K9O_LLM_MODEL_UNAVAILABLE` (404) when the requested model identifier is not in the curated library (every listed model is available on every plan), and `K9O_LLM_PROVIDER_UNAVAILABLE` (503) when an external provider is unreachable.

## Parameters

| Name | In | Required | Description |
|---|---|---|---|
| `modelId` | path | yes | The model's id, as the model list returns it. Example: `"mistral-7b"`. |
| `projectSlug` | path | yes | The project's slug: the path segment after `@`. It is unique within the org. Example: `"storefront"`. |

## Request body

| Field | Type | Required | Description |
|---|---|---|---|
| `prompt` | string | yes | The prompt, sent to the model as a single user message. 1 to 100,000 characters. |
| `maxTokens` | integer | no | The most tokens the model may generate, 1 to 32,000. Default: the model server's own limit. |
| `temperature` | number | no | Sampling temperature, 0 to 2. Lower is more deterministic. Default: the model server's own. |
| `usageType` | string | no | A label of your own for this call, echoed back in the response. Default `sdk.direct`. |

```json
{
  "prompt": "Summarise this ticket in one sentence: the export button does nothing on Safari.",
  "maxTokens": 256,
  "temperature": 0.2,
  "usageType": "support.summarise"
}
```

## Responses

- **200**: Success

| Field | Type | Required | Description |
|---|---|---|---|
| `data` | object | yes | The model's reply and token usage. |
| `data.modelId` | string | yes | The model that answered. |
| `data.content` | string | yes | The model's reply. Empty when the model returned no text. |
| `data.finishReason` | object | yes | Why the model stopped, as the model server reports it: usually `stop` (it finished) or `length` (it reached `maxTokens`). Null when not reported. |
| _one of_ | | |  |
| _or_ | | |  |
| `data.usage` | object | yes | Token counts reported by the model server. Only reported counts are metered. |
| `data.usage.promptTokens` | integer | yes | Tokens in the prompt. 0 when the model server didn't report it. |
| `data.usage.completionTokens` | integer | yes | Tokens generated. 0 when the model server didn't report it. |
| `data.usage.totalTokens` | integer | yes | Prompt and generated tokens together. 0 when the model server didn't report it. |
| `data.usageType` | string | yes | The `usageType` you sent, or `sdk.direct`. |
| `meta` | object | yes |  |


## Errors

| HTTP | Code | When |
|---|---|---|
| 400 | `K9O_MODEL_NOT_DECLARED` | The model identifier is not declared in the extension's manifest compatibility list. |
| 401 | `K9O_INVALID_CREDENTIALS` | The request credentials are missing or invalid. |
| 401 | `K9O_SESSION_EXPIRED` | Your session has expired; sign in again to continue. |
| 402 | `K9O_OUT_OF_TOKENS` | The org's included inference credit is used up and its wallet cannot cover the call (W9-Billing's inference gate). Never raised for your own provider (BYOM). |
| 403 | `K9O_ACCESS_DENIED` | Your role does not permit direct LLM inference invocation; PROJECT_AUTHOR or PROJECT_ADMIN is required. |
| 502 | `K9O_UPSTREAM_UNAVAILABLE` | The inference credit check is unavailable, so the model was not called; retry. |
| 503 | `K9O_LLM_PROVIDER_UNAVAILABLE` | The external LLM provider is temporarily unreachable; please retry with exponential backoff. |

## Code samples

### cURL

```bash
curl -X POST 'https://<org>.api-<region>.k9o.io/@storefront/compute/llms/<modelId>/invoke' \
  -H 'Authorization: Bearer $K9O_TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
  "prompt": "Summarise this ticket in one sentence: the export button does nothing on Safari.",
  "maxTokens": 256,
  "temperature": 0.2,
  "usageType": "support.summarise"
}'
```

### fetch

```javascript
const res = await fetch('https://<org>.api-<region>.k9o.io/@storefront/compute/llms/<modelId>/invoke', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${process.env.K9O_TOKEN}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
  "prompt": "Summarise this ticket in one sentence: the export button does nothing on Safari.",
  "maxTokens": 256,
  "temperature": 0.2,
  "usageType": "support.summarise"
}),
});
const { data, meta } = await res.json();
```

### k9o CLI

```bash
k9o function invoke <name>
```

### @k9o/core

```typescript
// const k9o = new K9o({ orgSlug: 'playground', projectSlug: 'storefront', apiKey: 'k9o_…' })
const { data } = await k9o.functions.invoke('mistral-7b', {
  prompt: 'Summarise this ticket in one sentence: the export button does nothing on Safari.',
  maxTokens: 256,
  temperature: 0.2,
  usageType: 'support.summarise'
})
```

### k9o

```python
# k9o = create_client(org_slug="playground", project_slug="storefront", api_key="k9o_…")
response = k9o.functions.invoke("mistral-7b", {
    "prompt": "Summarise this ticket in one sentence: the export button does nothing on Safari.",
    "maxTokens": 256,
    "temperature": 0.2,
    "usageType": "support.summarise"
})
```

### sdk-go

```go
// ctx := context.Background()
// client, _ := k9o.NewClient(k9o.ClientOpts{OrgSlug: "playground", ProjectSlug: "storefront", APIKey: "k9o_…"})
data, _, kerr := client.Functions.Invoke(ctx, "mistral-7b", map[string]any{"prompt": "Summarise this ticket in one sentence: the export button does nothing on Safari.", "maxTokens": 256, "temperature": 0.2, "usageType": "support.summarise"})
```

### k9o

```rust
// let k9o = Client::new(ClientOpts { org_slug: "playground".into(), project_slug: "storefront".into(), api_key: "k9o_…".into() })?;
let resp = k9o.functions.invoke("mistral-7b", json!({"prompt": "Summarise this ticket in one sentence: the export button does nothing on Safari.", "maxTokens": 256, "temperature": 0.2, "usageType": "support.summarise"})).await?
```

### dev.k9o:core

```java
// K9o k9o = K9o.createClient(K9o.Opts.builder().orgSlug("playground").projectSlug("storefront").apiKey("k9o_…").build());
var response = k9o.functions().invoke("mistral-7b", Map.of("prompt", "Summarise this ticket in one sentence: the export button does nothing on Safari.", "maxTokens", 256, "temperature", 0.2, "usageType", "support.summarise"))
```

### K9o.Core

```c#
// var k9o = K9oClient.Create(new K9oOptions { OrgSlug = "playground", ProjectSlug = "storefront", ApiKey = "k9o_…" });
var response = await k9o.Functions.InvokeAsync("mistral-7b", new Dictionary<string, object> { ["prompt"] = "Summarise this ticket in one sentence: the export button does nothing on Safari.", ["maxTokens"] = 256, ["temperature"] = 0.2, ["usageType"] = "support.summarise" })
```

### k9o

```ruby
# k9o = K9o.create_client(org_slug: "playground", project_slug: "storefront", api_key: "k9o_…")
response = k9o.functions.invoke("mistral-7b", { prompt: "Summarise this ticket in one sentence: the export button does nothing on Safari.", maxTokens: 256, temperature: 0.2, usageType: "support.summarise" })
```

### k9o/sdk

```php
// $k9o = K9oClient::create(['org_slug' => 'playground', 'project_slug' => 'storefront', 'api_key' => 'k9o_…']);
$response = $k9o->functions->invoke('mistral-7b', ['prompt' => 'Summarise this ticket in one sentence: the export button does nothing on Safari.', 'maxTokens' => 256, 'temperature' => 0.2, 'usageType' => 'support.summarise'])
```

### K9o

```swift
// let k9o = try K9o.createClient(orgSlug: "playground", projectSlug: "storefront", apiKey: "k9o_…")
let result = try await k9o.functions.invoke("mistral-7b", {
  prompt: "Summarise this ticket in one sentence: the export button does nothing on Safari.",
  maxTokens: 256,
  temperature: 0.2,
  usageType: "support.summarise"
})
```

### core-kotlin

```kotlin
// val k9o = createClient { orgSlug = "playground"; projectSlug = "storefront"; apiKey = "k9o_…" }
val result = k9o.functions.invoke("mistral-7b", {
  prompt: "Summarise this ticket in one sentence: the export button does nothing on Safari.",
  maxTokens: 256,
  temperature: 0.2,
  usageType: "support.summarise"
})
```

### k9o (Flutter)

```dart
// final k9o = K9o.createClient(orgSlug: 'playground', projectSlug: 'storefront', apiKey: 'k9o_…');
final result = await k9o.functions.invoke('mistral-7b', {
  prompt: 'Summarise this ticket in one sentence: the export button does nothing on Safari.',
  maxTokens: 256,
  temperature: 0.2,
  usageType: 'support.summarise'
})
```

### @k9o/react-native

```javascript
// const k9o = createClient({ orgSlug: 'playground', projectSlug: 'storefront', apiKey: 'k9o_…' })  // RN, secure storage
const { data } = await k9o.functions.invoke('mistral-7b', {
  prompt: 'Summarise this ticket in one sentence: the export button does nothing on Safari.',
  maxTokens: 256,
  temperature: 0.2,
  usageType: 'support.summarise'
})
```


Canonical page: https://docs.kilimanjaro.dev/api-playground/core/invoke-llm
