Node.js Implementation of Cody Chat API - max_tokens Parameter Not Recognized

I’ve implemented a Node.js server to interact with the Cody Chat API, using Cody Chat to explain me how. While the connection and authentication work (200 status), the API consistently reports “max_tokens: Field required” despite the parameter being present in the request.

Setup details:

Request body:
{
“messages”: [{
“speaker”: “human”,
“text”: “test”
}],
“max_tokens”: 2000
}

parameters like temperature: 0.2, top_k: 1, top_p: 1 were used too, but are considered optional now, right?

The API returns 200 but then streams:
event: error
data: {“error”:“Sourcegraph Cody Gateway: unexpected status code 400: {"type":"error","error":{"type":"invalid_request_error","message":"max_tokens: Field required"}}”}

Tested variations:

  • Different max_tokens values (500, 1000, 2046)
  • Various parameter formats (max_tokens, maxTokens)
  • Different request structures

Full response headers and rate limiting info available if needed.

Has anyone successfully implemented the Cody Chat API in Node.js?

Update: Python Implementation Shows Same Behavior

To verify this isn’t Node.js specific, I tested with Python 3.9.2 using the requests library:

import requests
import json

CODY_API_URL = "https://sourcegraph.com/.api/completions/stream"
headers = {
    "Content-Type": "application/json",
    "Accept": "text/event-stream",
    "Authorization": "token [valid_token]"
}

data = {
    "messages": [{
        "speaker": "human",
        "text": "test message"
    }],
    "max_tokens": 2000
}

Yet returns the same error:

event: error
data: {"error":"Sourcegraph Cody Gateway: unexpected status code 400: {\"type\":\"error\",\"error\":{\"type\":\"invalid_request_error\",\"message\":\"max_tokens: Field required\"}}"}

Hey @QueasoNogni

the Cody API is not official supported and a ban might apply if not handled correctly. If you want to see how the API works for different languages, have a look here: sourcegraph/openapi: :construction: Work-in-progress - OpenAPI specs for Sourcegraph APIs

Hi! I focus on getting good results and am not aware of boundaries and rules I need to take into consideration. Open voor more hints