Key takeaways
- The OpenAI API bills per input and output token by model, with discounts for caching and batch jobs.
- Safety training and usage policies apply, and a free Moderation endpoint helps screen content.
- Streaming, smaller models and lower reasoning effort fix most slow responses.
- OPENAI_API_BASE is the old endpoint setting, replaced by base_url or OPENAI_BASE_URL.
- Keys are shown once, and API credits are separate from ChatGPT subscriptions.
The OpenAI API gives developers programmatic access to OpenAI's models for text, vision, audio, images and agents, billed per token from prepaid credits. Practical questions cover pricing, content rules, speed, configuration, keys, fine-tuning, images and token limits. I build apps with multi-provider fallback, such as RAG.NextUpgrad, often using OpenAI-compatible endpoints, so this page covers how the API charges, moderation, speed, OPENAI_API_BASE, buying credits, fine-tuning, creating keys, image input and tokens.
How does the OpenAI API charge?
The OpenAI API charges per token: input tokens you send and output tokens the model generates, at rates that differ by model. Cached input tokens cost less, and the Batch API offers discounts for asynchronous jobs.
Also asked as: how does openai api charge · can you train openai api
Some features add charges, such as built-in web search or file search calls, image generation and audio. Usage is deducted from prepaid credits on your account, and dashboards show spending by model and project. Set budgets and notification thresholds to avoid surprises, and review monthly usage reports to spot waste. Reduce costs by choosing smaller models for simple tasks, trimming prompts, using structured outputs to shorten responses, and caching repeated context. Check the pricing page for current rates.
Is the OpenAI API censored?
OpenAI's models follow usage policies and safety training, so they refuse some harmful requests, such as instructions for serious harm. Developers cannot disable this training, but within policy the API is flexible for a wide range of legitimate applications.
Also asked as: is openai api censored
OpenAI provides a free Moderation endpoint to screen inputs and outputs in your application. Refusals sometimes affect legitimate uses, such as medical or security education, so give clear context about the purpose and audience, and document refusal cases you encounter to improve prompts. Applications must also follow the usage policies, and violations can lead to account suspension. Open-weight models offer more control for specialised needs, but you then take full responsibility for safety and misuse prevention.
Why is the OpenAI API slow?
Slowness usually comes from larger or reasoning models that think before answering, long prompts, long outputs, not using streaming, high reasoning effort settings, rate limiting or network latency. Peak demand can also add delay.
Also asked as: why openai api is slow
Enable streaming so users see output immediately, and choose smaller models for simple tasks. For reasoning models, lower the reasoning effort when tasks are easy. Keep prompts concise and reuse stable prefixes to benefit from prompt caching. Limit maximum output tokens to what you need. For background work, the Batch API trades speed for cost savings. Monitor latency by model, and add fallbacks to other providers for reliability, as RAG.NextUpgrad does.
What is OPENAI_API_BASE?
OPENAI_API_BASE was an environment variable used by older versions of the OpenAI Python library to set the API endpoint URL. Newer versions use base_url in code or the OPENAI_BASE_URL environment variable instead.
Changing the base URL points the OpenAI SDK at another endpoint, such as Azure OpenAI, a corporate proxy, or OpenAI-compatible providers like Ollama, Groq, OpenRouter or vLLM servers. This lets the same code work with different backends. If older tutorials use OPENAI_API_BASE and your code ignores it, you are probably on a newer library version, so switch to base_url. Keep endpoints and keys in configuration, not hard-coded.
Where can you buy OpenAI API access?
Buy API credits on OpenAI's developer platform under billing settings, using a card, with automatic recharge available. Organisations can also access OpenAI models through Azure OpenAI, billed through Microsoft Azure.
Also asked as: where to buy openai api
API credits are separate from ChatGPT subscriptions, so a ChatGPT Plus plan does not include API usage. Start with a small credit purchase while prototyping, set spending limits, and monitor usage. Invoices help with reimbursement and accounting. Avoid third-party resellers offering cheap API keys, which are often stolen or shared, violate terms and can stop working at any moment. For enterprises, Azure procurement or direct agreements with OpenAI may offer better governance and billing options.
Can you train or fine-tune models through the OpenAI API?
Yes. You can fine-tune supported OpenAI models through the fine-tuning API, uploading examples in JSONL format. Options include supervised fine-tuning, preference fine-tuning and reinforcement fine-tuning for some reasoning models. You cannot train new models from scratch.
Fine-tuning suits consistent formats, styles, classification and specialised tasks. Prepare high-quality chat-formatted examples, upload training and validation files, start a job and monitor loss. The tuned model receives its own ID for API calls, with its own usage pricing. Keep a held-out set to compare tuned and base models. Before fine-tuning, test prompting and retrieval, which solve many needs faster. For full control over weights, open models fine-tuned with QLoRA, as in FineTune Studio, are an alternative.
How do you create an OpenAI API key?
Sign in to OpenAI's developer platform, open the API keys section, choose a project, and create a new secret key. Copy it immediately, since it is shown only once, and store it securely, such as in an OPENAI_API_KEY environment variable.
Also asked as: how create openai api key
Project-scoped keys limit access to a specific project's resources, and you can restrict permissions for each key. If you lose a key, create a new one and delete the old. Never paste keys into front-end code, public repositories or shared notebooks; leaked keys can be abused quickly. Rotate keys periodically and monitor usage for unexpected activity. Name keys clearly, such as by application and environment, to manage them later.
Can the OpenAI API read images?
Yes. Vision-capable OpenAI models accept images as inputs, by URL or base64 data, and can describe, compare, extract text from and reason about them. Separate image generation models create and edit images from prompts.
Also asked as: can openai api read images
Common uses include reading receipts, analysing charts and screenshots, classifying products and answering questions about photos. You can include several images in one request. Images consume input tokens based on size and detail settings, so choose lower detail when fine resolution is unnecessary. Verify extracted numbers, since vision models can misread small text. For scanned documents with complex layouts, dedicated OCR, such as the Mistral OCR my OCR & Speech Workspace uses, may be more reliable.
How many tokens can the OpenAI API handle?
Limits depend on the model: context windows range from tens of thousands of tokens for older models to hundreds of thousands or around a million for newer ones, and maximum output tokens vary separately. Check the models page for exact values.
Also asked as: how many tokens openai api
The context window covers your prompt, conversation history, documents and the response. Rate limits also cap tokens per minute for your organisation tier. Use the tiktoken library to count tokens before sending requests and to estimate costs. Long contexts cost more and can reduce answer quality for details buried in the middle, so retrieve only relevant passages. Split very large documents into chunks or use retrieval instead.
What are OpenAI API tokens?
Tokens are chunks of text that models read and generate; in English, a token averages about four characters, or roughly three quarters of a word. The API measures usage and bills in tokens, separately for input and output.
Tokens are not credentials and do not expire; they are units of text. Different languages tokenise differently, so Hindi and other Indian languages often use more tokens per word than English, increasing costs. Images and audio also convert to tokens. Count tokens with tiktoken, set maximum output limits, and keep prompts concise to control usage. Usage dashboards show token totals by model. Understanding tokens helps estimate costs and design applications that stay within context limits.





