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

# Code Completion

Generates code at a cursor position.

```http
POST /domains/models/capabilities/code-complete/execute
```

`prompt` is the code before the cursor. Optional `suffix` is the code that already exists after the cursor. The returned `completionText` belongs between them.

```text
prompt  +  completionText  +  suffix
```

## Request

#### TypeScript SDK

```ts
import { AgentRouterClient } from "@agentrouter/agentrouter";

const client = new AgentRouterClient({
  apiKey: process.env.AGENTIC_API_KEY,
});

const result = await client.models.code.complete.execute(
  {
    model: "deepseek-flash",
    prompt: "const label = ",
    suffix: "\nconsole.log(label);",
    max_tokens: 80,
    temperature: 0,
  },
  {
    routeKey: "models.code.complete.deepseek.mpp",
    allowFallback: false,
  },
);

console.log(result.completionText);
console.log(result.creditsCharged);
```

#### cURL

```bash
curl -X POST \
  "https://api.agentrouter.to/api/agentic-api/domains/models/capabilities/code-complete/execute" \
  -H "Authorization: Bearer $AGENTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-flash",
    "prompt": "const label = ",
    "suffix": "\nconsole.log(label);",
    "max_tokens": 80,
    "temperature": 0,
    "routeKey": "models.code.complete.deepseek.mpp",
    "allowFallback": false
  }'
```

### Capability input

| Field         | Type    | Required | Description                                                                                                                                       |
| ------------- | ------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `model`       | string  | yes      | Model accepted by the selected code route. The current DeepSeek route accepts `deepseek-flash`, `deepseek-v4-pro`, and the `deepseek-chat` alias. |
| `prompt`      | string  | yes      | Code before the cursor.                                                                                                                           |
| `suffix`      | string  | no       | Existing code after the cursor. The model fills the gap.                                                                                          |
| `max_tokens`  | integer | no       | Maximum generated tokens. Must be at least `1`; the DeepSeek wrapper default is `256`.                                                            |
| `temperature` | number  | no       | Sampling temperature from `0` to `2`. Use `0` for deterministic completion when possible.                                                         |

### Routing controls

| Field           | Type    | Required | Description                                         |
| --------------- | ------- | -------- | --------------------------------------------------- |
| `routeKey`      | string  | no       | Pin `models.code.complete.deepseek.mpp`.            |
| `provider`      | string  | no       | Provider hint. This route uses `deepseek`.          |
| `allowFallback` | boolean | no       | Allow another compatible code route when available. |

> **Note**
>
> Code Completion model IDs are route-specific. A model ID from the OpenRouter chat catalog is not automatically valid for this endpoint.

## Response

```json
{
  "success": true,
  "completionText": "\"agentRouterSmoke\";",
  "model": "deepseek-flash",
  "provider": "deepseek",
  "routeKey": "models.code.complete.deepseek.mpp",
  "fallbackUsed": false,
  "finishReason": "stop",
  "usage": {
    "promptTokens": 12,
    "completionTokens": 4,
    "totalTokens": 16
  },
  "creditsCharged": 3
}
```

The token counts and charge above are illustrative. Read them from the live response.

### Response fields

| Field            | Type    | Description                              |
| ---------------- | ------- | ---------------------------------------- |
| `completionText` | string  | Code inserted at the cursor.             |
| `model`          | string  | Code model used.                         |
| `provider`       | string  | Provider selected for execution.         |
| `routeKey`       | string  | Concrete route used.                     |
| `fallbackUsed`   | boolean | Whether execution used a fallback route. |
| `finishReason`   | string  | Why generation stopped when available.   |
| `usage`          | object  | Provider token usage when available.     |
| `creditsCharged` | number  | Authoritative final debit.               |

## Reconstruct the file

```ts
const completedFile = prompt + result.completionText + suffix;
```

For a simple continuation, omit `suffix`:

```ts
const result = await client.models.code.complete.execute(
  {
    model: "deepseek-flash",
    prompt: "export function slugify(value: string) {",
    max_tokens: 120,
    temperature: 0,
  },
  { routeKey: "models.code.complete.deepseek.mpp" },
);
```

## Inspect the exact route contract

```ts
const context = await client.catalog.routes.context(
  "models.code.complete.deepseek.mpp",
);

console.log(context.execute?.fields);
```

## Errors

| Status         | Meaning                                                   | Fix                                                  |
| -------------- | --------------------------------------------------------- | ---------------------------------------------------- |
| `400` or `422` | Missing `model` or `prompt`, or invalid parameter         | Compare the request with route context.              |
| `401`          | Missing or invalid AgentRouter API key                    | Send `Authorization: Bearer aak_...`.                |
| `402`          | Insufficient AgentRouter credits or payment-route funding | Add funds, then retry.                               |
| `404`          | Code model or route is unavailable                        | Refresh the route context and use a listed model ID. |
| `429`          | Provider or gateway rate limit                            | Retry with exponential backoff.                      |