Skip to main content

Setup

Create an API key and add balance. Install pip install openai and set NANOGPT_API_KEY in your environment. Never put your key in browser code or source control.

First, a complete response

The SDK base URL is https://api.nano-gpt.com/api/v1. This calls POST /api/v1/chat/completions.

Then, streaming

This example closes the HTTP connection even if parsing fails or you stop consuming the stream. The timeout bounds network operations, including time waiting for another chunk; it is not a total generation deadline.
The SDK parses SSE and raises for HTTP and in-stream error frames. Usage-only frames, empty deltas, reasoning and fragmented tool calls are handled separately. An early end without a finish reason is treated as an incomplete generation; malformed frames and read timeouts also fail explicitly. Reasoning and tool support depend on the selected model; this prompt does not request tools. The tool-call collector shows how to handle models that emit them; it never executes a tool. Keep the request ID printed before streaming when reporting an interrupted request. HTTP failures show the status, response body and request ID; avoid exposing raw error bodies or reasoning in public logs. A timeout or interrupted stream does not prove the request was unbilled. Inspect the failure before retrying; honor Retry-After on rate limits, and do not retry authentication or validation failures without correcting the request.

Other endpoints