Classes, objects and methods
Every AI SDK uses classes: client = Anthropic(), workflow = StateGraph(AgentState). Understanding classes lets you read SDK documentation confidently and build your own tools.
Class to objects
How a class blueprint creates multiple instances
# Basic class
class MenuItem:
def __init__(self, name, price, category="main"):
self.name = name
self.price = price
self.category = category
def describe(self):
return f"{self.name} (${self.price:.2f}) [{self.category}]"
# Create objects (instances)
burger = MenuItem("Mushroom Burger", 12.99)
bowl = MenuItem("Quinoa Power Bowl", 10.99, category="bowl")
print(burger.describe())
print(bowl.describe())
print(f"Burger price: {burger.price}")__init__ runs when you create an object. self refers to the instance. Methods are functions that belong to the class.
self is a reference to the current instance. When you call burger.describe(), Python passes the burger object as self automatically. This is how methods access the data stored in that specific instance. You never pass self yourself when calling methods.
class AgentConfig:
def __init__(self, role, model="gpt-4o-mini"):
self.role = role
self.model = model
self.history = []
def __str__(self):
return f"Agent({self.role}, model={self.model})"
def add_message(self, role, content):
self.history.append({"role": role, "content": content})
@property
def message_count(self):
return len(self.history)
agent = AgentConfig("SQL Expert")
print(agent) # Uses __str__
agent.add_message("user", "How many orders?")
print(f"Messages: {agent.message_count}") # No () needed__str__ controls how print() displays the object. @property makes a method look like an attribute. This is exactly how SDK clients work.
Quiz: Quiz
Loading practice…
Fill in the blanks: Complete the class
Loading practice…
Flashcards: Flashcards
Loading practice…
Validation checklist: Classes checklist
Loading practice…