AI AI Engineering

AI Engineering: APIs, Tools & Function Calling

What you will learn

How AI models are accessed via APIs, what endpoints and parameters matter, and how function/tool calling lets models interact with external systems.

OpenAI-compatible API

Most AI providers (OpenAI, Together, OpenRouter, OpenCode Go) use the same API format:

POST /v1/chat/completions
Authorization: Bearer sk-...
{
  "model": "deepseek-v4-flash",
  "messages": [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Hello!"}
  ],
  "temperature": 0.7,
  "max_tokens": 1024
}

Key parameters

Parameter What it controls Typical range
temperature Randomness / creativity 0.0 (deterministic) – 2.0 (very random)
max_tokens Maximum length of the response 1 – context limit
top_p Nucleus sampling (alternative to temperature) 0.0 – 1.0
presence_penalty Penalize repeating topics -2.0 – 2.0
frequency_penalty Penalize repeating tokens -2.0 – 2.0
stop Stop sequences ["\n\n", "END"]

Streaming

Instead of waiting for the full response, streaming sends tokens one by one as they are generated:

{"choices": [{"delta": {"content": "Hello"}}]}
{"choices": [{"delta": {"content": "! How"}}]}
{"choices": [{"delta": {"content": " can I help?"}}]}

This is what gives chat interfaces their typewriter effect. Set stream: true in the request body.

Function / tool calling

Models can request to call external functions. You define the function schema, the model decides when to call it:

{
  "tools": [{
    "type": "function",
    "function": {
      "name": "get_weather",
      "description": "Get current temperature for a city",
      "parameters": {
        "type": "object",
        "properties": {
          "city": {"type": "string"}
        },
        "required": ["city"]
      }
    }
  }]
}

Common mistakes

  • Hardcoding API keys in source code — use .env files or environment variables.
  • Forgetting to set max_tokens — the model stops at the default (often 256), truncating your response.
  • Using temperature = 0 for creative tasks — it will be repetitive. Use 0.7–0.9 for creative writing.
  • Not handling API errors — your code should handle rate limits (429), auth errors (401), and timeouts gracefully.
  • Committing .env to git — always .gitignore it (this course's repo does!).

Quick check below!