Building Your First AI Application With APIs

You do not need to train a model to build an AI application. This module walks through building a working LLM-powered tool using an API - step by step.

From User to Builder

Every AI product you interact with is built on an infrastructure of APIs - programming interfaces that let developers send text to AI models and receive responses. You do not need to train a model to build an AI application. You need to connect to one that already exists.

This module walks through building a working AI tool from scratch, step by step.

What an API Is

An API (Application Programming Interface) is a standardized way for one piece of software to talk to another. When you use a weather app, it is talking to a weather data API. When you ask Siri a question, your phone is calling Apple's AI API.

For LLM APIs: you send text (your prompt) over the internet to the AI provider's server, and they send back text (the model's response). You pay a small amount per request. The model runs on their infrastructure - you never need to manage it.

The most common LLM APIs are Anthropic (Claude), OpenAI (GPT), and Google (Gemini). Access, credits, and free-tier policies change frequently, so check each provider's current pricing and console limits before designing around a free tier.

Setting Up

First, get an API key. Go to console.anthropic.com, create an account, and generate an API key. Store it safely - treat it like a password.

Install the Python library:

pip install anthropic

Your First API Call

python
import anthropic client = anthropic.Anthropic(api_key="your-api-key-here") message = client.messages.create( model="claude-haiku-4-5-20251001", # Fast, inexpensive model for development max_tokens=1024, messages=[ {"role": "user", "content": "Summarize the following text in three bullet points:\n\nPaste your text here."} ] ) print(message.content[0].text)

Run this, and you have called an LLM API. Everything from here is building on this foundation.

Building a Document Summarizer

python
import anthropic client = anthropic.Anthropic(api_key="your-api-key-here") def summarize_document(text: str, audience: str = "general") -> str: prompt = f"""Summarize the following document for a {audience} audience. Return exactly three bullet points. Each bullet should be one sentence. Focus on the most important information. Document: {text}""" response = client.messages.create( model="claude-haiku-4-5-20251001", max_tokens=512, messages=[{"role": "user", "content": prompt}] ) return response.content[0].text # Test it sample_text = """ Paste a long article or document here... """ summary = summarize_document(sample_text, audience="executive") print(summary)

Test this on a document from your work. Change the audience parameter and observe how the summary changes.

Building a Customer FAQ Responder

python
import anthropic client = anthropic.Anthropic(api_key="your-api-key-here") FAQ_CONTEXT = """ We are TechCorp. Our product is a project management tool. Pricing: Basic ($10/month), Pro ($25/month), Enterprise (custom pricing). Free trial: 14 days, no credit card required. Support: Email [email protected], response within 24 hours. Cancellation: Cancel anytime, no refund for current period. """ def answer_faq(question: str) -> str: prompt = f"""You are a helpful customer support agent for TechCorp. Answer the customer's question using only the information provided below. If the answer is not in the information provided, say "I don't have that information - please email [email protected]." Be concise and friendly. Company information: {FAQ_CONTEXT} Customer question: {question}""" response = client.messages.create( model="claude-haiku-4-5-20251001", max_tokens=256, messages=[{"role": "user", "content": prompt}] ) return response.content[0].text # Test with sample questions test_questions = [ "How much does the Pro plan cost?", "Can I cancel my subscription?", "Do you offer a student discount?", ] for q in test_questions: print(f"Q: {q}") print(f"A: {answer_faq(q)}") print()

The third question tests the out-of-scope behavior - the model should say it does not have that information rather than making something up.

Building a Simple Interactive Interface

python
import anthropic client = anthropic.Anthropic(api_key="your-api-key-here") def run_assistant(): print("AI Assistant - type 'quit' to exit\n") conversation = [] while True: user_input = input("You: ").strip() if user_input.lower() == 'quit': break if not user_input: continue conversation.append({"role": "user", "content": user_input}) response = client.messages.create( model="claude-haiku-4-5-20251001", max_tokens=1024, system="You are a helpful assistant. Be concise and accurate.", messages=conversation ) assistant_reply = response.content[0].text conversation.append({"role": "assistant", "content": assistant_reply}) print(f"Assistant: {assistant_reply}\n") run_assistant()

This maintains conversation history so the model can refer to earlier messages in the conversation.

Deploying Your Tool

For a personal tool running locally: what you have built is already usable. For sharing with others:

  • Streamlit (streamlit.io): Turn a Python script into a web app with minimal code. Free hosting for public apps.
  • Gradio: Similar to Streamlit, optimized for AI interfaces. Used by many research demos.

Both let you go from a working Python script to a shareable web URL in under an hour.

Where to Go Next

The next module explains RAG - the pattern that extends what you built here from answering general questions to answering questions about specific documents or knowledge bases.

Common Mistakes

Hardcoding API keys in code. An API key embedded in source code will eventually be committed to version control, shared in a screenshot, or exposed in a build artifact. Once a key is in a git history it should be considered compromised and rotated immediately. Always load secrets from environment variables or a secrets manager - never from string literals in source files.

Not handling API errors or rate limits. LLM APIs return HTTP 429 (rate limit), 500 (server error), and timeout responses under normal production conditions. An app that treats these as crashes will fail unpredictably for users. Implement retry with exponential backoff for transient errors and surface a user-friendly error message rather than a stack trace.

Building without a fallback for when the API is unavailable. A third-party API can go down, have elevated latency, or exceed your budget cap. If your app has no graceful degradation path - a cached response, a simpler fallback model, a "service temporarily unavailable" message - users experience complete outages rather than degraded service. Define your fallback behavior before you go live.

What to Practice Next

  • Move every API key in your first AI app to an environment variable and verify the app still works; check your git history to confirm the key was never committed.
  • Add error handling and exponential backoff retry logic to your primary API call; test it by temporarily using an invalid key to trigger a 401 error.
  • Define and implement a fallback response for the case where the API returns an error; verify it is triggered and looks reasonable to a user.

Module 17 of 25 · Curious to AI-Fluent

Related Posts

More posts

AI Agents: What They Are, What They Can Do, and How They Go Wrong

An agent is an AI that takes actions, not just answers questions. That changes what safe use looks like. Learn in plain English what agents are, how they connect to your tools, why they can be tricked by what they read, and the one question to ask before letting one act for you.

#ai-literacy#agents#prompt-injection#mcp#llm