# AI Platform

## Obtaining API Key

Navigate to AI [configuration management](https://console.littlepig.cloud/config_manage) page from AI API Integration Tab. Click on "Open Service" button to enter configuration page and follow up with the confirmation.

<figure><img src="/files/YlJpc5wCxjRnjG3py12X" alt=""><figcaption></figcaption></figure>

&#x20;To obtain the API key click on "View". You may have to enter your account password for verification.

## Configuring AI API

**API Domain**: `https://sgai.littlepig.cloud`\
**Endpoint**: `/anthropic/v1/messages`\
**Protocol**: HTTPS\
**Request Method**: POST\
**Request Body**: Serialized JSON format

### Request Structure

#### Required Headers

| Header         | Type   | Description                          |
| -------------- | ------ | ------------------------------------ |
| `x-api-key`    | string | Authentication key from the Console. |
| `Content-Type` | string | Must be `application/json`.          |

#### Request Data Definition

| Parameter     | Type      | Required | Description                                                         |
| ------------- | --------- | -------- | ------------------------------------------------------------------- |
| `model`       | string    | **Yes**  | Claude model to use.                                                |
| `max_tokens`  | integer   | **Yes**  | Maximum tokens to generate.                                         |
| `messages`    | object\[] | **Yes**  | Conversation history (see **Messages Format**).                     |
| `stream`      | boolean   | No       | Stream the response as Server-Sent Events.                          |
| `system`      | string    | No       | System-level prompt.                                                |
| `temperature` | number    | No       | 0.0‑1.0; lower = more deterministic, higher = more creative.        |
| `top_p`       | number    | No       | Nucleus‑sampling ratio. Adjust either `temperature` **or** `top_p`. |
| `tools`       | object\[] | No       | Definitions of external tools the model may call.                   |

***

### Single Use Case – Basic Chat

```bash
curl https://sgai.littlepig.cloud/anthropic/v1/messages   -H "x-api-key: $API_KEY"   -H "anthropic-version: 2023-06-01"   -H "Content-Type: application/json"   -d '{
    "model": "claude_37_sonnet",
    "max_tokens": 1024,
    "messages": [
      { "role": "user", "content": "Hello, world" }
    ]
  }'
```

***

### Messages Format

#### Text‑only (shorthand)

```json
{ "role": "user", "content": "Hello, Claude" }
```

#### Explicit Content Blocks

Each input message content may be either a single string or an array of content blocks, where each block has a specific type. Using a string for content is shorthand for an array of one content block of type "text". The following input messages are equivalent:

```json
 {"role": "user", "content": "Hello, Claude"}
```

```json
{ "role": "user", "content": [ { "type": "text", "text": "Hello, Claude" } ] }
```

#### Multiple Conversational Turns

```json
[
  { "role": "user", "content": "Hello there." },
  { "role": "assistant", "content": "Hi, I'm Claude. How can I help you?" },
  { "role": "user", "content": "Can you explain LLMs in plain English?" }
]
```

#### Partially‑filled Response

```json
[
  { "role": "user", "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun" },
  { "role": "assistant", "content": "The best answer is (" }
]
```

#### Image + Text

```json
{
  "role": "user",
  "content": [
    {
      "type": "image",
      "source": {
        "type": "base64",
        "media_type": "image/jpeg",
        "data": "/9j/4AAQSkZJRg..."
      }
    },
    { "type": "text", "text": "What is in this image?" }
  ]
}
```

Supported image types: `image/jpeg`, `image/png`, `image/gif`, `image/webp`.

***

### **Tool Definition Schema**

If you include tools in your API request, the model may return tool\_use content blocks that represent the model's use of those tools. You can then run those tools using the tool input generated by the model and then optionally return results back to the model using tool\_result content blocks.

| Field          | Type   | Required | Description                     |
| -------------- | ------ | -------- | ------------------------------- |
| `name`         | string | **Yes**  | Tool name.                      |
| `description`  | string | No       | What the tool does.             |
| `input_schema` | object | **Yes**  | JSON schema for the tool input. |

```json
[
    {
        "name": "get_stock_price",
        "description": "Get the current stock price for a given ticker symbol.",
        "input_schema": {
            "type": "object",
            "properties": {
                "ticker": {
                    "type": "string",
                    "description": "The stock ticker symbol, e.g. AAPL for Apple Inc."
                }
            },
            "required": [
                "ticker"
            ]
        }
    }
]
```

***

### Response Data Definition

| Field                 | Description                               |
| --------------------- | ----------------------------------------- |
| `content`             | Content blocks produced by Claude.        |
| `id`                  | Message ID.                               |
| `model`               | Model that generated the reply.           |
| `role`                | Always `assistant`.                       |
| `stop_reason`         | Why generation stopped (e.g. `end_turn`). |
| `usage.input_tokens`  | Tokens in the request.                    |
| `usage.output_tokens` | Tokens in the response.                   |

#### Example

```json
{
    "id": "msg_bdrk_01FTuLJMy9aYU5TQhK14rMsE",
    "type": "message",
    "role": "assistant",
    "model": "claude-3-7-sonnet-20250219",
    "content": [
        {
            "type": "text",
            "text": "Hello! How can I assist you today? Feel free to ask me any questions or let me know what you'd like to talk about."
        }
    ],
    "stop_reason": "end_turn",
    "stop_sequence": null,
    "usage": {
        "input_tokens": 10,
        "cache_creation_input_tokens": 0,
        "cache_read_input_tokens": 0,
        "output_tokens": 31
    }
}
```

***

### Streaming Events (`stream=true`)

| Event                 | Payload                           |
| --------------------- | --------------------------------- |
| `content_block_delta` | Partial token or tool‑input data. |
| `error`               | Error details.                    |

Example:

```
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"ello frien"}}

event: error
data: {"type":"error","error":{"type":"overloaded_error","message":"Overloaded"}}
```

Tool invocation stream:

```
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{"location":"San Fra"}"}}
```

***

### Tool Invocation Walk‑through

```bash
curl https://api.anthropic.com/v1/messages \
  -H "content-type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "claude-3-7-sonnet-20250219",
    "max_tokens": 1024,
    "tools": [
      {
        "name": "get_weather",
        "description": "Get the current weather in a given location",
        "input_schema": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "The city and state, e.g. San Francisco, CA"
            }
          },
          "required": ["location"]
        }
      }
    ],
    "messages": [
      {
        "role": "user",
        "content": "What is the weather like in San Francisco?"
      }
    ]
  }'
```

***

### Further Reading

<https://docs.anthropic.com/en/api/messages>

### Pricing

{% hint style="info" %}
The model pricing is subject to change. Please visit the [configuration management](https://console.littlepig.cloud/config_manage) page to view the latest pricing.
{% endhint %}

| Model              | Price / 1 000 input tokens | Price / 1 000 output tokens |
| ------------------ | -------------------------- | --------------------------- |
| claude\_3\_haiku   | $0.00025                   | $0.00125                    |
| claude\_3\_sonnet  | $0.003                     | $0.015                      |
| claude\_3\_opus    | $0.015                     | $0.075                      |
| claude\_35\_haiku  | $0.0008                    | $0.004                      |
| claude\_35\_sonnet | $0.003                     | $0.015                      |
| claude\_37\_sonnet | $0.003                     | $0.015                      |

{% hint style="warning" %}
To disable API access, click on "Close Service" button in [configuration management](https://vps.littlepig.tech/config_manage) page.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.littlepig.cloud/documentations/ai-platform.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
