Building your first LLM API call in Python (step by step)
10 hours ago
- An LLM is a stateless prediction machine relying entirely on provided context.
- This build, 'First Contact', is a Python script that makes an API call to Anthropic's Claude, prints the response, and logs token usage.
- Prerequisites include a terminal, Python, an Anthropic API key with credit, and a code editor.
- Setup involves creating a project folder, setting up a virtual environment, and installing libraries 'anthropic' and 'python-dotenv'.
- The script loads the API key from a .env file using dotenv, creates an Anthropic client, sends a message request, and extracts the text and token counts from the response.
- The final code is provided, demonstrating a complete call with model 'claude-haiku-4-5', max_tokens 1024, and a user message.
- The response shows the model's prediction and token usage (e.g., 19 input tokens, 51 output tokens, 70 total), illustrating how the context window works.