Skip to content

Chat Completions

POST /v1/chat/completions

OpenAI-compatible chat completions. Target either:

  • a model you added
  • one of your own published agents by agent name
  • a shared published agent by canonical id shared/<ownerUsername>/<slug>

Request

Body (essential fields):

  • model (string) — Your model name, your own published agent name, or a shared published agent id such as shared/john_doe/customer-support-bot.
  • messages (array) — Items of { role: "system" | "user" | "assistant" | "tool", content: string }.

Optional OpenAI-style parameters:

  • temperature, max_tokens, top_p, n, stop, tools, tool_choice, stream.
  • response_format or responseFormat for structured output hints.
  • extra_params or extraParams for provider-specific passthrough options.

MissionSquad-specific parameters (snake_case and camelCase both accepted where noted):

  • sessionId (camelCase only) — correlate tool-call events and streaming usage; falls back to the x-session-id header.
  • providerState — provider resume state returned by a previous turn (e.g. for stateful Anthropic containers); pass it back to continue.
  • request_timeout_ms / requestTimeoutMs — integer, 11800000.
  • max_retries / maxRetries — integer, 010.

For a model target, remaining OpenAI-style sampling fields (temperature, max_tokens/ maxTokens, top_p/topP, stop, frequency_penalty, presence_penalty, etc.) are passed through to the provider. For an agent target, per-request sampling overrides are ignored — the agent's own configuration governs.

Programmatic tool calling (PTC) for Claude is not a request parameter. It is configured on the model — see Models → Programmatic tool calling.

Notes:

  • model may be:
    • a user model name
    • an owned published agent name
    • a shared published agent id in the form shared/<ownerUsername>/<slug>
  • Tool usage: Provide tools in the OpenAI function/tool format:
    • type: "function"
    • function: { name: string; description?: string; parameters: JSONSchema }
  • Streaming: Set stream: true for Server-Sent Events (SSE). The API sends OpenAI-like delta chunks (text/event-stream).

Structured output and provider params

POST /v1/chat/completions accepts both snake_case and camelCase for structured output and passthrough params:

  • response_format or responseFormat
  • extra_params or extraParams

Supported response_format shapes:

  • Text mode:
    • { "type": "text" }
  • JSON object mode:
    • { "type": "json_object", "schema": { ...optional JSON schema object... } }
  • JSON schema mode:
    • { "type": "json_schema", "json_schema": { "schema": { ...required schema object... } } }

Notes:

  • If response_format is not in a valid shape, it is ignored and the request continues without structured output hints.
  • Actual schema enforcement is provider/model-specific.

Examples

JavaScript (fetch, non‑streaming)

ts
const res = await fetch("https://agents.missionsquad.ai/v1/chat/completions", {
  method: "POST",
  headers: {
    "x-api-key": process.env.MSQ_API_KEY!,
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    model: "my-gpt4",
    messages: [
      { role: "system", content: "You are helpful." },
      { role: "user", content: "Write a haiku about summer." }
    ],
    temperature: 0.7
  })
});
const data = await res.json();
console.log(data.choices[0].message.content);

JavaScript (Node 20+, streaming via fetch and readable body)

ts
const res = await fetch("https://agents.missionsquad.ai/v1/chat/completions", {
  method: "POST",
  headers: {
    "x-api-key": process.env.MSQ_API_KEY!,
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    model: "my-gpt4",
    messages: [
      { role: "system", content: "You are helpful." },
      { role: "user", content: "Stream a short story in 2 paragraphs." }
    ],
    stream: true
  })
});

if (!res.ok || !res.body) throw new Error(`HTTP ${res.status}`);

const reader = res.body.getReader();
const decoder = new TextDecoder();
while (true) {
  const { value, done } = await reader.read();
  if (done) break;
  const chunk = decoder.decode(value);
  // Stream is SSE: lines prefixed with "data: ..."
  for (const line of chunk.split("\n")) {
    if (line.startsWith("data: ")) {
      const payload = line.slice(6).trim();
      if (payload === "[DONE]") break;
      try {
        const event = JSON.parse(payload);
        // event is OpenAI-like chat.completion.chunk
        // consume event.choices[0].delta?.content, etc.
      } catch {}
    }
  }
}

OpenAI SDK (non‑streaming)

ts
import OpenAI from "openai";
const client = new OpenAI({ apiKey: process.env.MSQ_API_KEY, baseURL: "https://agents.missionsquad.ai/v1" });

const completion = await client.chat.completions.create({
  model: "my-gpt4-or-agent-name",
  messages: [
    { role: "user", content: "Summarize this in 3 bullet points: ..." }
  ],
  // tools: [{ type: "function", function: { name, description, parameters: { type: "object", properties: {}, required: [] } } }]
});
console.log(completion.choices[0].message);

JavaScript (fetch, structured output)

ts
const res = await fetch("https://agents.missionsquad.ai/v1/chat/completions", {
  method: "POST",
  headers: {
    "x-api-key": process.env.MSQ_API_KEY!,
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    model: "my-gpt4",
    messages: [{ role: "user", content: "Return title and summary for this article." }],
    response_format: {
      type: "json_schema",
      json_schema: {
        schema: {
          type: "object",
          properties: {
            title: { type: "string" },
            summary: { type: "string" }
          },
          required: ["title", "summary"],
          additionalProperties: false
        }
      }
    },
    extra_params: {
      // provider-specific passthrough values
      seed: 12345
    }
  })
});

const data = await res.json();
console.log(data.choices?.[0]?.message?.content);

Optional Headers

  • x-client-id: arbitrary client identifier for event correlation.
  • x-session-id: provide to correlate tool-call events and streaming usage metrics within a session.

Cancel a Streaming Chat

POST /v1/chat/cancel

Cancel an in-flight chat stream by sessionId. If you also know the backing runId, include it for a broader cancellation lookup.

Body:

ts
{
  sessionId: string
  runId?: string
}

Validation behavior:

  • 400 if sessionId is missing or blank

Example:

ts
const res = await fetch("https://agents.missionsquad.ai/v1/chat/cancel", {
  method: "POST",
  headers: {
    "x-api-key": process.env.MSQ_API_KEY!,
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    sessionId: "chat-session-123",
    runId: "run_abc123"
  })
});

const data = await res.json();
console.log(data);

Response:

json
{
  "success": true,
  "found": true,
  "cancelled": true,
  "alreadyCancelled": false
}

Notes:

  • found indicates whether a matching active stream or run was located
  • cancelled indicates that a live stream was cancelled during this request
  • alreadyCancelled indicates that a matching target had already been cancelled before this request
  • when only sessionId is supplied, the API also checks whether that session maps to a known run and cancels that run if present

See also