---
title: "Code Completion"
canonical: https://www.quantconnect.com/docs/v2/cloud-platform/api-reference/ai-assistance/tools/code-completion
markdown: https://www.quantconnect.com/docs/v2/cloud-platform/api-reference/ai-assistance/tools/code-completion.md
language: python
source: https://github.com/QuantConnect/Documentation/tree/master/01%20Cloud%20Platform/99%20API%20Reference/10%20AI%20Assistance/02%20Tools/02%20Code%20Completion
modified: 2026-10-28
index: https://www.quantconnect.com/llms.txt
---

# Code Completion

[Documentation](https://www.quantconnect.com/docs/v2.md) > [Cloud Platform](https://www.quantconnect.com/docs/v2/cloud-platform.md) > [API Reference](https://www.quantconnect.com/docs/v2/cloud-platform/api-reference.md) > [AI Assistance](https://www.quantconnect.com/docs/v2/cloud-platform/api-reference/ai-assistance.md) > [Tools](https://www.quantconnect.com/docs/v2/cloud-platform/api-reference/ai-assistance/tools.md) > Code Completion

> Code examples are shown in Python. Append `?lang=csharp` to this URL for the C# version of this page.

## Introduction

Show the code completion for a specific text input.

## Request

Show the code completion for a specific text input. The `/ai/tools/complete` API accepts requests in the following format:

| `CodeCompletionRequest` Model - Request to show code completion for a specific text input. |  |
| --- | --- |
| language | `string Enum required example: Py` Programming language for the code completion. Options : ['C#', 'Py'] |
| sentence | `string required example: self.add_eq` Sentence to complete. |
| responseSizeLimit | `integer example: 10` Maximum size of the responses. |
| Example | `{ "language": "Py", "sentence": "self.add_eq", "responseSizeLimit": 10 }` |

## Responses

The `/ai/tools/complete` API provides a response in the following format:

### 200 Success

| `CodeCompletionResponse` Model - Response to a code completion request. |  |
| --- | --- |
| state | `string Enum` State of the code completion. Options : ['End'] |
| version | `number example: 2.0` Version of the response. |
| payload | `string Array` Code completion suggestions. |
| payloadType | `string example: StringArray` Type of the payload. |
| Example | `{ "state": "End", "version": 2.0, "payload": [ "string" ], "payloadType": "StringArray" ] }` |

### 401 Authentication Error

| `UnauthorizedError` Model - Unauthorized response from the API. Key is missing, invalid, or timestamp is too old for hash. |  |
| --- | --- |
| www_authenticate | `string` Header |

## Examples

The following example demonstates code completion for a specific algorithm attribute through the cloud API.

```
from base64 import b64encode
from hashlib import sha256
from time import time
from requests import get, post
BASE_URL = 'https://www.quantconnect.com/api/v2/'

# You need to replace these with your actual credentials.
# You can request your credentials at https://www.quantconnect.com/settings/
# You can find our organization ID at https://www.quantconnect.com/organization/
USER_ID = 0
API_TOKEN = '____'
ORGANIZATION_ID = '____'

def get_headers():
    # Get timestamp
    timestamp = f'{int(time())}'
    time_stamped_token = f'{API_TOKEN}:{timestamp}'.encode('utf-8')

    # Get hased API token
    hashed_token = sha256(time_stamped_token).hexdigest()
    authentication = f'{USER_ID}:{hashed_token}'.encode('utf-8')
    authentication = b64encode(authentication).decode('ascii')

    # Create headers dictionary.
    return {
        'Authorization': f'Basic {authentication}',
        'Timestamp': timestamp
    }

# Authenticate to verify credentials
response = post(f'{BASE_URL}/authenticate', headers = get_headers())
print(response.json())

# --------------------

### Code Completion
# Send a POST request to the /ai/tools/complete endpoint to get code completion
response = post(f'{BASE_URL}/ai/tools/complete', headers=get_headers(), json={
    "language": "Python",  # Programming language for code completion
    "sentence": "self.add_equity('AAPL",  # Partial code to complete
    "responseSizeLimit": 30  # Maximum size of the completion response
})
# Parse the JSON response
result = response.json()
# Check if the request was successful and print the code completion results
if result['success']:
    print("Code Completion Results:")
    print(result)
```
