AI Courses

Talking to an AI with an API

Written by

in

Talking to an AI with an API | Master AI Automation in 4 hours Master AI Automation in 4 hours Course About Ayush Modules Sample chapter Toolbox The Microcap Minute Classroom / Module 04: APIs: How Software Talks / Chapter 5 Talking to an AI with an API Watch first, then read. Same lesson, your pace. What you will learn – The one API call behind every AI app – A working script you write yourself – The off-peak rule for cheaper, smoother usage Every AI app is this one call ChatGPT, Kimi’s app, a support bot on a shop site, all of them are a thin wrapper around the same request: send messages, receive completion . Once you can make that call, you can build any of them. The code (finally!) Inside your api-lab venv (Chapters 3-4), install the helper and create ask.py : uv pip install openai python-dotenv from dotenv import load_dotenv # loads .env from Chapter 3 import os from openai import OpenAI load_dotenv() client = OpenAI( base_url=”https://openrouter.ai/api/v1″, # one door, many models api_key=os.environ[“OPENROUTER_API_KEY”], # never hardcoded! ) reply = client.chat.completions.create( model=”qwen/qwen3-30b-a3b:free”, # a free model via OpenRouter messages=[ {“role”: “system”, “content”: “You explain things to a 15-year-old in India. Short sentences.”}, {“role”: “user”, “content”: “What is an API?”} ], ) print(reply.choices[0].message.content) Run it: python ask.py An explanation arrives, generated by a model you called directly , bypassing any chat interface entirely. Swap the model string for any OpenRouter model ID and the same script drives Claude, GPT or Kimi. That single line is the whole multi-model idea in miniature. Read the script once more and see Modules 1-2 inside it: the system message is R-C-T-F as configuration; messages is the context desk; the reply is prediction. The off-peak rule APIs have busy hours, everyone’s scripts hammer them at the same times, so free-tier queues lengthen and paid tokens cost the same regardless. The professional habit: Batch your API work into off-peak windows. For Indian users, global traffic is quietest roughly 11pm-8am IST . Practically: Schedule bulk jobs overnight ( cron at 2am, or just run before bed) Save interactive experiments for whenever; save loops (100 summaries, weekly reports) for quiet hours Off-peak = fewer rate-limit errors, faster responses, and free tiers simply feel bigger This one scheduling habit costs nothing and reliably doubles what your quota buys. Try it yourself Get ask.py running end to end. Then personalise it twice: (1) change the system prompt to your custom instructions from Module 2; (2) change messages[-1] ‘s content via input: add q = input(“Ask: “) and send that instead. You now own a chatbot with your personality on a free model. Run it once at 2am too, feel the difference. Save the script; Module 6 wires it into automation. Key takeaways – All AI apps reduce to one call: messages in, completion out. – base_url + api_key + model + messages = the whole recipe; swap models by swapping one string. – Keys come from .env , never from inside code. – Off-peak batching (โ‰ˆ11pm-8am IST) makes quotas stretch dramatically. Download the exercise sheet (PDF) Module workbook (PDF) โ† Prev: First contact: curl Next: Free models forever โ†’ Classroom / Module 04: APIs: How Software Talks / Chapter 5 Talking to an AI with an API What you will learn – The one API call behind every AI app – A working script you write yourself – The off-peak rule for cheaper, smoother usage Every AI app is this one call ChatGPT, Kimi’s app, a support bot on a shop site, all of them are a thin wrapper around the same request: send messages, receive completion . Once you can make that call, you can build any of them. The code (finally!) Inside your api-lab venv (Chapters 3-4), install the helper and create ask.py : uv pip install openai python-dotenv from dotenv import load_dotenv # loads .env from Chapter 3 import os from openai import OpenAI load_dotenv() client = OpenAI( base_url=

๐Ÿ“„ Download PDF

๐Ÿ“„ Download PDF