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
In this repository14 other files it gives its agents.
- arcgis-enterprise-k8sSkill
- code-reviewSkill
- confluence-to-nextjsSkill
- k8s-nextjs-deploySkill
- kilo-gatewaySkill
- login-govSkill
- loki-best-practicesSkill
- magnificSkill
- messages-api-sdkSkill
- nextjs-monorepo-ciSkill
- openrouterSkill
- postgres-opsSkill
- session-handoffSkill
- ubuntu24-stigSkill
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
- Distinctive: line 22
Before writing any API code, verify in order:
- Very specific: line 28
JSON mode: if using response_format={"type": "json_object"}, the word "JSON" must appear in your messages or the API errors
- Very specific: line 322
Embeddings: use text-embedding-3-small unless you need max accuracy
- Very specific: line 323
Check
finish_reason: before accessing tool_calls or content
≈ “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.
Files
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:
- Key present:
echo $OPENAI_API_KEYreturns a value starting withsk- - SDK installed:
python -c "import openai; print(openai.__version__)"succeeds (need ≥ 1.0.0 for the new client) - tiktoken available:
pip show tiktoken— needed for token counting; not auto-installed withopenai - Model ID exact:
gpt-4onotgpt4o;gpt-4o-mininotgpt-4-mini— wrong IDs return 404, not a fallback - 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 floatsToken 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 totalModel Selection
| Model | Input $/Mtok | Output $/Mtok | Context | Best For |
| gpt-6-sol | $2 | $10 | 1.05M | General coding, tool use — best value |
| gpt-6-luna | $0.10 | $0.50 | 1.05M | High-volume, simple tasks |
| gpt-6-astra | $10 | $50 | 1.05M | Complex reasoning, hardest problems |
| text-embedding-3-small | $0.02 | — | 8K | Semantic search, similarity |
Legacy, still-active models:
| Model | Input $/Mtok | Output $/Mtok | Context | Notes |
| gpt-4o | $2.50 | $10 | 128K | Prior-gen general/vision/tool-use model |
| gpt-4o-mini | $0.15 | $0.60 | 128K | Prior-gen low-cost model |
| o3 | $2 | $8 | 200K | Reasoning model; o3-2025-04-16 snapshot shuts down 2026-12-11 |
| o1 | $15 | $60 | 200K | Its 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_000Key Parameters
| Parameter | Default | Notes |
max_tokens | unlimited | Always set to control cost |
temperature | 1.0 | 0.0–0.3 for code/analysis; not supported on o1/o3 |
top_p | 1.0 | Alternative to temperature; don't use both |
seed | None | Integer → deterministic output (for testing) |
response_format | text | {"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:
- Setting
temperatureon o1 or o3 — reasoning models ignoretemperature; the parameter is accepted but has no effect, so any "deterministic reasoning" code you write around it is a no-op. - 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. - 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. - 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.
- Calling
tiktoken.encoding_for_model()without a fallback — it raisesKeyErrorfor any model newer than your installed tiktoken: o1/o3 needtiktoken>=0.9.0, o4-mini>=0.10.0, gpt-5>=0.12.0(gpt-5-*>=0.11.0), and no release mapsgpt-6-*yet. CatchKeyErrorand usetiktoken.get_encoding("o200k_base")(ascount_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. - Not checking
finish_reasonbefore accessingtool_calls— iffinish_reasonis"length"instead of"tool_calls",message.tool_callsisNoneand your code crashes. - Streaming with structured JSON mode —
stream=Trueandresponse_format={"type":"json_object"}together give you fragmented JSON chunks; you must buffer the full stream beforejson.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_reason | tool_call.name | Action |
"tool_calls" | get_order_status | Look up order, append result, continue |
"tool_calls" | escalate_to_human | Create ticket, append result, continue |
"stop" | — | Final answer — print and done |
"length" | — | Response cut — raise max_tokens |
Best Practices
- Always set
max_tokens— default is unlimited and expensive - 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
- Count tokens before the call using tiktoken for batch cost prediction
- Use
seedfor reproducible outputs in tests - Temperature 0.0 for code generation; skip temperature for o1/o3 (they ignore it)
- JSON mode requires the word "JSON" somewhere in the messages
- Embeddings: use
text-embedding-3-smallunless you need max accuracy - Check
finish_reasonbefore accessingtool_callsorcontent
Official Docs
- Python library: https://github.com/openai/openai-python
- Chat completions: https://platform.openai.com/docs/api-reference/chat
- Models: https://platform.openai.com/docs/models
- Embeddings: https://platform.openai.com/docs/guides/embeddings
- tiktoken: https://github.com/openai/tiktoken
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
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.

