Artificial Intelligence

Introduction to large language models (LLMs) and the Anthropic API

Anna Nowak 01.10.2026 539 wyświetleń 3 komentarzy
This translation was generated by AI and may contain inaccuracies. Read the original (PL)

What is an LLM?

A Large Language Model (LLM) is a machine learning model trained on huge text corpora. It can:

  • generate text, code, translations
  • answer questions in natural language
  • analyze, summarize and classify documents
  • write and debug code

The most popular models

Model Vendor Context Strengths
Claude Opus 4 Anthropic 200k tokens Reasoning, code, safety
Claude Sonnet 4 Anthropic 200k tokens Price/quality balance
GPT-4o OpenAI 128k tokens Multimodality
Gemini 1.5 Pro Google 1M tokens Long context
Llama 3.1 Meta 128k tokens Open-source

Calling Claude through the API

Python

import anthropic

client = anthropic.Anthropic(api_key="sk-ant-...")

message = client.messages.create(
    model="claude-opus-4-5",
    max_tokens=1024,
    system="You are a PHP and Symfony expert.",
    messages=[
        {"role": "user", "content": "Explain in 3 points what Dependency Injection is"}
    ]
)

print(message.content[0].text)

PHP (Guzzle)

use GuzzleHttp\Client;

$client = new Client(['base_uri' => 'https://api.anthropic.com']);

$response = $client->post('/v1/messages', [
    'headers' => [
        'x-api-key'         => $_ENV['ANTHROPIC_API_KEY'],
        'anthropic-version' => '2023-06-01',
        'content-type'      => 'application/json',
    ],
    'json' => [
        'model'      => 'claude-opus-4-5',
        'max_tokens' => 1024,
        'messages'   => [
            ['role' => 'user', 'content' => 'Write a PHP function to validate a VAT number']
        ],
    ],
]);

$data = json_decode($response->getBody(), true);
echo $data['content'][0]['text'];

Streaming responses

Instead of waiting for the whole response, you can stream token by token:

with client.messages.stream(
    model="claude-opus-4-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write an essay about PHP"}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Common use cases in web projects

  1. SEO description generation — paste the title and content, get a meta description
  2. Code assistant — code review, error explanations
  3. Support chatbot — answers questions based on an FAQ
  4. Sentiment analysis — comment classification
  5. Translations — better than Google Translate for technical text

Costs: Claude Sonnet is ~$3/1M input tokens. A typical blog article is about 1500 tokens. Reasonable for assistants, expensive when processing millions of documents.


Good practices

  • always validate and sanitize user input
  • never paste sensitive data (passwords, API keys) into the prompt
  • set max_tokens — without a limit the model can generate very long responses
  • use the system prompt to assign a role and constraints to the model
Tagi: #Python #AI #REST API
Udostępnij: Facebook X LinkedIn

Komentarze (3)


J
Jan Kowalski 21.10.2026, 14:27

Przykład z Anthropic API działa jak marzenie. Polecam też tryb streaming!

P
Piotr Wiśniewski 21.10.2026, 14:27

Testuję Claude do generowania testów jednostkowych — wyniki rewelacyjne.

A
Anna Nowak 21.10.2026, 14:27

Ja używam go do code review. Oszczędza mnóstwo czasu w PR-ach.

Zaloguj się, aby dodać komentarz.