M
MeshWorld.
Cheatsheet OpenAI GPT-4o ChatGPT AI LLM API Developer Tools 6 min read

OpenAI API Cheat Sheet: GPT-4o, Tools & Assistants

By Vishnu Damwala

Quick reference tables

Models

Model IDContextBest for
gpt-4o128K tokensMultimodal, speed + quality balance
gpt-4o-mini128K tokensFast, cheap, everyday tasks
o3200K tokensComplex reasoning, math, coding
o4-mini200K tokensFast reasoning at lower cost
gpt-4.5-preview128K tokensCreative writing, nuanced conversation
text-embedding-3-large8K tokensHigh-quality embeddings
text-embedding-3-small8K tokensCheaper embeddings
dall-e-3Image generation
whisper-1Audio transcription
tts-1Text-to-speech

Chat Completions — key parameters

ParameterTypeWhat it does
modelstringModel to use
messagesarray[{role, content}] — system/user/assistant
max_completion_tokensintMax output tokens
temperaturefloat 0–2Randomness (0 = deterministic)
top_pfloat 0–1Nucleus sampling
streamboolStream tokens as server-sent events
toolsarrayFunction definitions for tool use
tool_choicestring/objauto, none, required, or specific tool
response_formatobject{type: "json_object"} or json_schema
seedintReproducible outputs (best effort)
storeboolStore conversation for fine-tuning
nintNumber of completions to generate
stopstring/arrayStop sequences

Roles in messages

RolePurpose
systemInstructions, persona, context
userHuman turn
assistantModel turn (for multi-turn)
toolTool result (returned after function call)

Assistants API — key concepts

ConceptWhat it is
AssistantA configured agent with model, tools, instructions
ThreadA conversation session (stores messages)
MessageA user or assistant message in a Thread
RunExecuting an Assistant on a Thread
Run StepIndividual actions within a Run
Vector StoreStorage for file search (RAG)

Embeddings

ParameterValue
EndpointPOST /v1/embeddings
Best modeltext-embedding-3-large (3072 dims)
Cheap modeltext-embedding-3-small (1536 dims)
Max input8191 tokens
Use caseSemantic search, RAG, similarity

Image generation (DALL-E 3)

ParameterOptions
modeldall-e-3
size1024x1024, 1792x1024, 1024x1792
qualitystandard, hd
stylevivid, natural
n1 (DALL-E 3 supports only 1)

Detailed sections

Basic chat completion (Node.js)

import OpenAI from "openai";

const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

const response = await client.chat.completions.create({
  model: "gpt-4o",
  messages: [
    { role: "system", content: "You are a helpful coding assistant." },
    { role: "user", content: "Explain the difference between == and === in JavaScript." },
  ],
  max_completion_tokens: 512,
});

console.log(response.choices[0].message.content);

Streaming

const stream = await client.chat.completions.create({
  model: "gpt-4o",
  stream: true,
  messages: [{ role: "user", content: "Write a haiku about databases." }],
});

for await (const chunk of stream) {
  const text = chunk.choices[0]?.delta?.content || "";
  process.stdout.write(text);
}

Structured output (JSON schema)

const response = await client.chat.completions.create({
  model: "gpt-4o",
  messages: [
    { role: "user", content: "Extract: John Doe, [email protected], Senior Engineer" },
  ],
  response_format: {
    type: "json_schema",
    json_schema: {
      name: "contact",
      schema: {
        type: "object",
        properties: {
          name: { type: "string" },
          email: { type: "string" },
          title: { type: "string" },
        },
        required: ["name", "email", "title"],
        additionalProperties: false,
      },
      strict: true,
    },
  },
});

const contact = JSON.parse(response.choices[0].message.content);

Function calling / tool use

const tools = [
  {
    type: "function",
    function: {
      name: "search_database",
      description: "Search the product database",
      parameters: {
        type: "object",
        properties: {
          query: { type: "string" },
          limit: { type: "integer", default: 10 },
        },
        required: ["query"],
      },
    },
  },
];

const response = await client.chat.completions.create({
  model: "gpt-4o",
  tools,
  tool_choice: "auto",
  messages: [{ role: "user", content: "Find products under $50" }],
});

const toolCall = response.choices[0].message.tool_calls?.[0];
if (toolCall) {
  const args = JSON.parse(toolCall.function.arguments);
  // call your actual function: searchDatabase(args.query, args.limit)
}
// Create embeddings
const embedding = await client.embeddings.create({
  model: "text-embedding-3-small",
  input: "How do I reset my password?",
});

const vector = embedding.data[0].embedding; // float array

// Compare two texts (cosine similarity)
function cosineSimilarity(a, b) {
  const dot = a.reduce((sum, val, i) => sum + val * b[i], 0);
  const magA = Math.sqrt(a.reduce((sum, val) => sum + val * val, 0));
  const magB = Math.sqrt(b.reduce((sum, val) => sum + val * val, 0));
  return dot / (magA * magB);
}

Assistants API — simple agent

// 1. Create an Assistant (do this once)
const assistant = await client.beta.assistants.create({
  name: "Code Reviewer",
  instructions: "Review code for bugs, style issues, and security vulnerabilities.",
  model: "gpt-4o",
  tools: [{ type: "code_interpreter" }],
});

// 2. Create a Thread per user session
const thread = await client.beta.threads.create();

// 3. Add user message
await client.beta.threads.messages.create(thread.id, {
  role: "user",
  content: "Review this function: function add(a,b){return a+b}",
});

// 4. Run the Assistant
const run = await client.beta.threads.runs.createAndPoll(thread.id, {
  assistant_id: assistant.id,
});

// 5. Get the response
if (run.status === "completed") {
  const messages = await client.beta.threads.messages.list(thread.id);
  console.log(messages.data[0].content[0].text.value);
}

Image generation

const image = await client.images.generate({
  model: "dall-e-3",
  prompt: "A futuristic server room with glowing blue circuits",
  size: "1792x1024",
  quality: "hd",
  style: "vivid",
  n: 1,
});

console.log(image.data[0].url);

Audio transcription (Whisper)

import fs from "fs";

const transcription = await client.audio.transcriptions.create({
  model: "whisper-1",
  file: fs.createReadStream("recording.mp3"),
  language: "en", // optional
  response_format: "text",
});

console.log(transcription);

Text-to-speech

import fs from "fs";

const audio = await client.audio.speech.create({
  model: "tts-1",
  voice: "nova", // alloy, echo, fable, onyx, nova, shimmer
  input: "Hello, this is a test of the text to speech API.",
  speed: 1.0, // 0.25–4.0
});

const buffer = Buffer.from(await audio.arrayBuffer());
fs.writeFileSync("output.mp3", buffer);

Environment setup

# Install SDK
npm install openai

# Set API key
export OPENAI_API_KEY=sk-...

# Python SDK
pip install openai

python3 -c "
import openai
client = openai.OpenAI()
r = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[{'role':'user','content':'Hello'}]
)
print(r.choices[0].message.content)
"