agentleFS
andreab67andreab67/agent-skills/openai-sdk/SKILL.md

openai-sdk

Skill1 starsChanged 2 months agoBSD-3-ClauseView on GitHub

Expert help with the OpenAI Python SDK (openai package): chat completions, function calling / tools, structured JSON output, embeddings, tiktoken token counting, cost estimation, and model selection (GPT-6 Sol/Luna/Astra plus legacy gpt-4o, gpt-4o-mini, o3). Use when calling OpenAI models via the Chat Completions API, implementing tool-call loops, generating embeddings, pre-flight token counting with tiktoken, or estimating request cost. Do NOT use for Azure OpenAI deployment config, the OpenRouter or Kilo gateways, or the Anthropic SDK - those have their own skills.

  • Reads credentials
  • Installs packages

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 1 is distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Before writing any API code, verify in order:

    line 22
  2. Very specific:

    JSON mode: if using response_format={"type": "json_object"}, the word "JSON" must appear in your messages or the API errors

    line 28
  3. Very specific:

    Embeddings: use text-embedding-3-small unless you need max accuracy

    line 322
  4. Very specific:

    Check finish_reason: before accessing tool_calls or content

    line 323

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

OpenAI SDK

Expert assistance with the OpenAI Python SDK (openai package). Covers chat completions, function calling, structured outputs, embeddings, token counting with tiktoken, cost estimation, and model selection.

When to Use This Skill

  • Calling GPT-6 Sol/Luna/Astra, or legacy GPT-4o, GPT-4o-mini, or o3 models
  • Implementing function calling / tools
  • Generating embeddings for semantic search
  • Counting tokens with tiktoken before making a call
  • Estimating costs for a request
  • Using structured outputs (JSON mode / response_format)
  • Building streaming chat applications

Pre-flight Checklist

Before writing any API code, verify in order:

  1. Key present: echo $OPENAI_API_KEY returns a value starting with sk-
  2. SDK installed: python -c "import openai; print(openai.__version__)" succeeds (need ≥ 1.0.0 for the new client)
  3. tiktoken available: pip show tiktoken — needed for token counting; not auto-installed with openai
  4. Model ID exact: gpt-4o not gpt4o; gpt-4o-mini not gpt-4-mini — wrong IDs return 404, not a fallback
  5. JSON mode: if using response_format={"type": "json_object"}, the word "JSON" must appear in your messages or the API errors

Quick Reference

Installation

pip install openai tiktoken
export OPENAI_API_KEY='sk-...'

Basic Chat Completion

from openai import OpenAI

client = OpenAI()  # reads OPENAI_API_KEY

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are a senior Python engineer."},
        {"role": "user", "content": "Write a binary search function."},
    ],
    max_tokens=1024,
    temperature=0.2,
)
print(response.choices[0].message.content)
print(f"Tokens: {response.usage.prompt_tokens} in / {response.usage.completion_tokens} out")

Streaming

with client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Explain async generators"}],
    stream=True,
) as stream:
    for chunk in stream:
        delta = chunk.choices[0].delta.content
        if delta:
            print(delta, end="", flush=True)

Function Calling / Tools

tools = [
    {
        "type": "function",
        "function": {
            "name": "query_database",
            "description": "Run a read-only SQL query and return results as JSON",
            "parameters": {
                "type": "object",
                "properties": {
                    "sql": {"type": "string"},
                    "limit": {"type": "integer", "default": 100}
                },
                "required": ["sql"]
            }
        }
    }
]

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "List the top 5 customers by revenue"}],
    tools=tools,
    tool_choice="auto",
)

# Handle tool call
if response.choices[0].finish_reason == "tool_calls":
    tool_call = response.choices[0].message.tool_calls[0]
    import json
    args = json.loads(tool_call.function.arguments)
    result = run_query(args["sql"])  # your impl

    messages = [
        {"role": "user", "content": "List top 5 customers by revenue"},
        response.choices[0].message,  # assistant's tool_call message
        {"role": "tool", "tool_call_id": tool_call.id, "content": json.dumps(result)}
    ]
    final = client.chat.completions.create(model="gpt-4o", messages=messages)
    print(final.choices[0].message.content)

Structured Output (JSON Mode)

import json

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{
        "role": "user",
        "content": "Extract: name, version, language from 'FastAPI 0.115.5 Python framework'. Return JSON."
    }],
    response_format={"type": "json_object"},
)
data = json.loads(response.choices[0].message.content)

Embeddings

response = client.embeddings.create(
    model="text-embedding-3-small",  # 1536 dims, cheapest
    input="FastAPI async endpoint with PostgreSQL",
)
vector = response.data[0].embedding  # list of 1536 floats

Token Counting with tiktoken

import tiktoken

enc = tiktoken.encoding_for_model("gpt-4o")
tokens = enc.encode("Your prompt text here")
print(f"Token count: {len(tokens)}")

# Estimate for a message list
def count_message_tokens(messages: list[dict], model: str = "gpt-4o") -> int:
    try:
        enc = tiktoken.encoding_for_model(model)
    except KeyError:  # model newer than your tiktoken (e.g. gpt-6-*): o200k_base is the current family
        enc = tiktoken.get_encoding("o200k_base")
    total = 3  # reply priming
    for m in messages:
        total += 4  # per-message overhead
        total += len(enc.encode(m.get("content", "")))
        total += len(enc.encode(m.get("role", "")))
    return total

Model Selection

ModelInput $/MtokOutput $/MtokContextBest For
gpt-6-sol$2$101.05MGeneral coding, tool use — best value
gpt-6-luna$0.10$0.501.05MHigh-volume, simple tasks
gpt-6-astra$10$501.05MComplex reasoning, hardest problems
text-embedding-3-small$0.02—8KSemantic search, similarity

Legacy, still-active models:

ModelInput $/MtokOutput $/MtokContextNotes
gpt-4o$2.50$10128KPrior-gen general/vision/tool-use model
gpt-4o-mini$0.15$0.60128KPrior-gen low-cost model
o3$2$8200KReasoning model; o3-2025-04-16 snapshot shuts down 2026-12-11
o1$15$60200KIts only snapshot, o1-2024-12-17, shuts down 2026-10-23 — avoid for new work

Coding recommendation: GPT-6 Sol for general coding tasks; GPT-6 Astra for the hardest reasoning/architecture problems; GPT-6 Luna for autocomplete/linting/classification at scale. Prefer the GPT-6 line over the legacy gpt-4o/o-series models for new work — o1 is shutting down 2026-10-23.

Cost Estimation

def estimate_cost(prompt_tokens: int, completion_tokens: int, model: str = "gpt-6-sol") -> float:
    prices = {
        # current models
        "gpt-6-sol":     (2.00,  10.00),
        "gpt-6-luna":    (0.10,  0.50),
        "gpt-6-astra":   (10.00, 50.00),
        # legacy, still-active models
        "gpt-4o":        (2.50,  10.00),
        "gpt-4o-mini":   (0.15,  0.60),
        "o3":            (2.00,  8.00),
        "o1":            (15.00, 60.00),  # o1-2024-12-17 shuts down 2026-10-23
    }
    if model not in prices:
        raise ValueError(f"unknown model {model!r}; add it to prices")
    in_p, out_p = prices[model]
    return (prompt_tokens * in_p + completion_tokens * out_p) / 1_000_000

Key Parameters

ParameterDefaultNotes
max_tokensunlimitedAlways set to control cost
temperature1.00.0–0.3 for code/analysis; not supported on o1/o3
top_p1.0Alternative to temperature; don't use both
seedNoneInteger → deterministic output (for testing)
response_formattext{"type": "json_object"} for guaranteed JSON
tool_choice"auto""required" forces a tool call

Error Handling

from openai import OpenAI, RateLimitError, BadRequestError, AuthenticationError

try:
    response = client.chat.completions.create(...)
except RateLimitError:
    time.sleep(60)  # retry after back-off
except BadRequestError as e:
    print(f"Bad request: {e}")  # malformed input, context too long
except AuthenticationError:
    print("Invalid API key")

Anti-patterns

These all look reasonable but will silently fail or waste money:

  1. Setting temperature on o1 or o3 — reasoning models ignore temperature; the parameter is accepted but has no effect, so any "deterministic reasoning" code you write around it is a no-op.
  2. Using response_format={"type": "json_object"} without the word "JSON" in the messages — the API raises a 400; you must include "JSON" (case-insensitive) somewhere in your system or user message.
  3. Omitting the assistant tool-call message before the tool result — when appending the conversation after a tool call, you must include response.choices[0].message (the assistant turn) before the {"role": "tool", ...} entry; skipping it causes a 400.
  4. Embedding batch size too large — the embeddings API accepts up to 2048 inputs per call; passing a larger list errors silently in some SDK versions and hard-errors in others. Chunk at ≤2048.
  5. Calling tiktoken.encoding_for_model() without a fallback — it raises KeyError for any model newer than your installed tiktoken: o1/o3 need tiktoken>=0.9.0, o4-mini >=0.10.0, gpt-5 >=0.12.0 (gpt-5-* >=0.11.0), and no release maps gpt-6-* yet. Catch KeyError and use tiktoken.get_encoding("o200k_base") (as count_message_tokens() above does). Either way, reasoning tokens are billed but never shown in the response, so a pre-flight tiktoken count is only a lower bound on actual billed tokens for these models.
  6. Not checking finish_reason before accessing tool_calls — if finish_reason is "length" instead of "tool_calls", message.tool_calls is None and your code crashes.
  7. Streaming with structured JSON mode — stream=True and response_format={"type":"json_object"} together give you fragmented JSON chunks; you must buffer the full stream before json.loads().

Worked Example — Function Calling with Triage Decisions

Scenario: a support agent that can look up order status or escalate to a human.

from openai import OpenAI
import json

client = OpenAI()

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_order_status",
            "description": "Return the current status and estimated delivery date for an order ID",
            "parameters": {
                "type": "object",
                "properties": {"order_id": {"type": "string"}},
                "required": ["order_id"],
            },
        },
    },
    {
        "type": "function",
        "function": {
            "name": "escalate_to_human",
            "description": "Open a support ticket and notify a human agent",
            "parameters": {
                "type": "object",
                "properties": {
                    "reason": {"type": "string"},
                    "customer_id": {"type": "string"},
                },
                "required": ["reason", "customer_id"],
            },
        },
    },
]

messages = [
    {"role": "system", "content": "You are a helpful support agent."},
    {"role": "user", "content": "Order #ORD-8821 hasn't arrived. It's been 3 weeks."},
]

response = client.chat.completions.create(
    model="gpt-4o", max_tokens=512, tools=tools, tool_choice="auto", messages=messages
)

# Check finish_reason BEFORE accessing tool_calls
if response.choices[0].finish_reason == "tool_calls":
    tool_call = response.choices[0].message.tool_calls[0]
    args = json.loads(tool_call.function.arguments)

    if tool_call.function.name == "get_order_status":
        result = lookup_order(args["order_id"])
    elif tool_call.function.name == "escalate_to_human":
        result = create_ticket(args["reason"], args["customer_id"])

    # Append BOTH: assistant turn, then tool result
    messages.append(response.choices[0].message)
    messages.append({"role": "tool", "tool_call_id": tool_call.id, "content": json.dumps(result)})

    final = client.chat.completions.create(model="gpt-4o", max_tokens=512, messages=messages)
    print(final.choices[0].message.content)

Loop triage table for this agent:

finish_reasontool_call.nameAction
"tool_calls"get_order_statusLook up order, append result, continue
"tool_calls"escalate_to_humanCreate ticket, append result, continue
"stop"—Final answer — print and done
"length"—Response cut — raise max_tokens

Best Practices

  1. Always set max_tokens — default is unlimited and expensive
  2. Use gpt-6-luna (or legacy gpt-4o-mini) for high-volume work that doesn't need a flagship model; it's ~20× cheaper than gpt-6-sol
  3. Count tokens before the call using tiktoken for batch cost prediction
  4. Use seed for reproducible outputs in tests
  5. Temperature 0.0 for code generation; skip temperature for o1/o3 (they ignore it)
  6. JSON mode requires the word "JSON" somewhere in the messages
  7. Embeddings: use text-embedding-3-small unless you need max accuracy
  8. Check finish_reason before accessing tool_calls or content

Official Docs

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.