> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.apologist.ai/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.apologist.ai/_mcp/server.

# Create Chat Completion

POST https://your-agent-domain.com/api/v1/chat/completions
Content-Type: application/json

Creates a chat completion using the agent's configured model. Supports both streaming and non-streaming responses.

Reference: https://docs.apologist.ai/agent-api/api-reference/chat-completions/create-chat-completion

## OpenAPI Specification

```yaml
openapi: 3.1.0
info:
  title: agent-api
  version: 1.0.0
paths:
  /chat/completions:
    post:
      operationId: createChatCompletion
      summary: Create Chat Completion
      description: >-
        Creates a chat completion using the agent's configured model. Supports
        both streaming and non-streaming responses.
      tags:
        - chat
      parameters:
        - name: x-api-key
          in: header
          description: API key for authentication
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ChatCompletionResponse'
        '422':
          description: Unprocessable Entity - Invalid request payload
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ChatCompletionRequest'
servers:
  - url: https://your-agent-domain.com/api/v1
    description: Production server
components:
  schemas:
    ChatCompletionRequest:
      oneOf:
        - description: Any type
        - description: Any type
      title: ChatCompletionRequest
    ChatMessageRole:
      type: string
      enum:
        - system
        - user
        - assistant
      title: ChatMessageRole
    ChatMessage:
      type: object
      properties:
        role:
          $ref: '#/components/schemas/ChatMessageRole'
        content:
          type: string
      title: ChatMessage
    ChatCompletionResponseChoicesItemsLogprobs:
      type: object
      properties: {}
      title: ChatCompletionResponseChoicesItemsLogprobs
    ChatCompletionResponseChoicesItems:
      type: object
      properties:
        index:
          type: integer
        message:
          $ref: '#/components/schemas/ChatMessage'
        logprobs:
          oneOf:
            - $ref: '#/components/schemas/ChatCompletionResponseChoicesItemsLogprobs'
            - type: 'null'
        finish_reason:
          type: string
      title: ChatCompletionResponseChoicesItems
    ChatCompletionResponseUsage:
      type: object
      properties:
        prompt_tokens:
          type: integer
        completion_tokens:
          type: integer
        total_tokens:
          type: integer
      title: ChatCompletionResponseUsage
    ChatCompletionResponse:
      type: object
      properties:
        id:
          type: string
        object:
          type: string
        created:
          type: integer
        model:
          type: string
        choices:
          type: array
          items:
            $ref: '#/components/schemas/ChatCompletionResponseChoicesItems'
        usage:
          $ref: '#/components/schemas/ChatCompletionResponseUsage'
        cached:
          type: boolean
      title: ChatCompletionResponse
    Error:
      type: object
      properties:
        success:
          type: boolean
        errors:
          type: array
          items:
            type: string
      required:
        - success
        - errors
      title: Error
  securitySchemes:
    ApiKeyHeader:
      type: apiKey
      in: header
      name: x-api-key
      description: API key for authentication
    BearerAuth:
      type: http
      scheme: bearer
      description: Bearer token authentication

```

## Examples



**Request**

```json
{
  "prompt": "string"
}
```

**Response**

```json
{
  "id": "string",
  "object": "chat.completion",
  "created": 1,
  "model": "string",
  "choices": [
    {
      "index": 1,
      "message": {
        "role": "system",
        "content": "string"
      },
      "logprobs": {},
      "finish_reason": "string"
    }
  ],
  "usage": {
    "prompt_tokens": 1,
    "completion_tokens": 1,
    "total_tokens": 1
  },
  "cached": true
}
```

**SDK Code**

```typescript
import { ApologistAgentClient } from "apologist";

async function main() {
    const client = new ApologistAgentClient({
        apiKey: "YOUR_API_KEY_HERE",
    });
    await client.chat.createChatCompletion({
        prompt: "string",
    });
}
main();

```

```python
from apologist import ApologistAgent

client = ApologistAgent(
    api_key="YOUR_API_KEY_HERE",
)

client.chat.create_chat_completion(
    request={"prompt": "string"},
)

```

```java
package com.example.usage;

import ai.apologist.AgentClient;
import ai.apologist.types.ChatCompletionRequest;
import java.util.HashMap;

public class Example {
    public static void main(String[] args) {
        AgentClient client = AgentClient
            .builder()
            .apiKey("YOUR_API_KEY_HERE")
            .build();

        client.chat().createChatCompletion(
            ChatCompletionRequest.of(new 
            HashMap<String, Object>() {{put("prompt", "string");
            }})
        );
    }
}
```

```ruby
require "apologist"

client = Apologist::AgentClient.new(api_key: "YOUR_API_KEY_HERE")

client.chat.create_chat_completion

```

```csharp
using Apologist;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace Usage;

public class Example
{
    public async Task Do() {
        var client = new AgentClient(
            apiKey: "YOUR_API_KEY_HERE"
        );

        await client.Chat.CreateChatCompletionAsync(
            new Dictionary<string, object>()
            {
                ["prompt"] = "string",
            }
        );
    }

}

```

```go
package example

import (
    context "context"

    apgsdkgo "github.com/apologist-project/apg-sdk-go"
    client "github.com/apologist-project/apg-sdk-go/client"
    option "github.com/apologist-project/apg-sdk-go/option"
)

func do() {
    client := client.NewApologistAgentClient(
        option.WithAPIKey(
            "YOUR_API_KEY_HERE",
        ),
    )
    request := &apgsdkgo.ChatCompletionRequest{
        Unknown: map[string]any{
            "prompt": "string",
        },
    }
    client.Chat.CreateChatCompletion(
        context.TODO(),
        request,
    )
}

```

```php
<?php

namespace Example;

use Apologist\AgentClient;

$client = new AgentClient(
    apiKey: 'YOUR_API_KEY_HERE',
);
$client->chat->createChatCompletion(
    [
        'prompt' => "string",
    ],
);

```

```swift
import Foundation

let headers = [
  "x-api-key": "<apiKey>",
  "Content-Type": "application/json"
]
let parameters = ["prompt": "string"] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://your-agent-domain.com/api/v1/chat/completions")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```