Python + AI API: A Step-by-Step Tutorial

By Darus S Alfatah · 2025-09-06 · Tutorial · 9 menit

Python + AI API: A Step-by-Step Tutorial

Author note: We always add timeouts and error handling from the first script, not as an afterthought.

Focus of this article

This article contains a step-by-step Python code walkthrough using the official SDK, including error handling and streaming.

Read also: Using an OpenAI-Compatible API Key in Your App · Node.js + Express: Building a Safe AI Proxy Endpoint

What You Need

Python 3.9 or newer, the official OpenAI SDK, and an API key from your dashboard. Store the key in an environment variable rather than in the script, because files get shared, committed, and screenshotted far more often than people expect.

Step-by-Step Setup

  1. Create a virtual environment and install the SDK.
  2. Export the key as an environment variable.
  3. Create a client pointing at the gateway base URL.
  4. Send a simple chat request and print the answer.
  5. Add a system message describing the task.
  6. Set a token limit and temperature appropriate to the task.
  7. Enable streaming for interactive use.
  8. Wrap the call in error handling with bounded retries.

Error Handling

Distinguish between failures. A 4xx usually means the request is wrong and retrying will not help; a timeout or 5xx deserves a retry with increasing delay. Cap attempts at three and log the reason for each failure so patterns become visible.

Saving Usage and Cost

Every response includes usage information. Store prompt and completion tokens alongside the feature name. After a week you will know which feature costs most, which is the only reliable basis for optimisation.

Testing Without Paying

For unit tests, replace the client with a stub that returns fixed text. Test your logic, not the provider. Run real integration tests manually before release, with a small set of representative prompts.

Frequently Asked Questions

Which model should I start with? The cheapest one that passes your quality check; upgrade only when it fails.

How do I handle long answers? Stream them and set a maximum token limit.

Can I use async? Yes, the SDK supports async clients; useful for serving many users.

How do I keep prompts organised? Keep them as constants or templates, not inline strings scattered through the code.

What if the provider changes behaviour? Keep a test set and run it after upgrades.

Conclusion

Integration is small; discipline is what makes it reliable. Environment variables, bounded retries, usage logging, and a test set cover most production needs.

Working With Longer Conversations

Chat history grows quickly and every request pays for it again. Keep only the last few turns plus a short summary of earlier ones. Summarise automatically when the history crosses a threshold, using the cheap model, and store the summary alongside the conversation so it survives restarts.

If your feature needs document context, retrieve relevant sections instead of appending the whole file. This single change usually reduces cost more than switching to a cheaper model.

Monitoring in Production

Log latency, token counts, and error categories per endpoint. Set an alert when daily spend exceeds a threshold or when the error rate climbs. Without alerts, a looping retry can quietly consume the entire monthly budget over a weekend.

Deployment Notes

Pin the SDK version so upgrades are deliberate. Keep secrets in the platform's secret manager, not in a committed file. Run a smoke test after every deployment that sends one request and verifies a non-empty response.

Keep a small script that runs your evaluation set and prints pass or fail. It takes an hour to build and saves days of guessing later.

An example of error handling: Wrap the call in try, set a timeout, and handle rate limits with a delay. Even a small script should survive transient failures.

Frequently Asked Questions

Can beginners follow this tutorial?

Yes. The steps are explained in order with code examples, suitable for beginners and experienced developers alike.

Is BankAI’s endpoint compatible with the library I already use?

Yes, BankAI is 100% OpenAI-compatible, so libraries such as the OpenAI SDK and LangChain work without code changes beyond the Base URL and API key.

Where do I get an API key to follow this tutorial?

You get the API key (and Secret Token) after registering and completing payment in the BankAI dashboard.

Sources & References

About the author

Darus S Alfatah — Founder & Writer

Darus S Alfatah is the founder of BankAI. He builds and operates the LLM API gateway that serves Indonesian developers, and writes about LLM integration, gateway architecture, and using AI practically and cost-effectively. Every guide is tested in a real environment — including code, configuration, and token calculations — before it is published, and updated when provider documentation or pricing changes.

Editorial policy

Related articles

🚀 Ready to Use the All-in-One API Key?

Sign up, top up with QRIS, and start building with ChatGPT, Claude, DeepSeek and every other model through one key.

🔑 SIGN UP NOW · Pricing