Calling a model from your own code

The request and response shapes of the three major APIs, and what survives a move between them.

What you will be able to do

Ship a feature that calls a model, streams the answer, and handles a tool call.

Prerequisites
What you are actually calling, or equivalent familiarity with tokens.
Length
7 lessons · 22 min

Syllabus

  1. 1Using SDKs DirectlyThe provider client sends the request and hands back the response, including the stop reason and usage figures a wrapper returning a string throws away. What you give up by adding a framework, and what you take on by not.3 min
  2. 2Claude Messages APIThe three required parameters include max_tokens, which is optional almost everywhere else. Content comes back as an array of typed blocks, so indexing the first one breaks the day you enable tools.3 min
  3. 3OpenAI Responses APIOpenAI recommends Responses for all new projects. Instructions move out of the message list, output arrives as a typed array rather than a string, and the server will hold the conversation for you.3 min
  4. 4Google Gemini APIFour safety categories, five thresholds, and a default of OFF. Blocking arrives in a field rather than an exception.3 min
  5. 5OpenAI-Compatible APIsOllama and vLLM both implement a subset of the OpenAI schema and both document what is missing. Unsupported fields are accepted and ignored, so a request that depends on tool_choice succeeds and quietly stops forcing anything.3 min
  6. 6Streaming ResponsesThe finish reason arrives late, and an error can land after a 200 with half an answer already on screen.3 min
  7. 7Function CallingThe model returns a request your code chooses to run. Arguments are model-generated, and providers document the model inventing ones nobody supplied.4 min