SynfraCore
Synfracore
Start Learning
Navigation

Academies

Platform

RoadmapsLabsCertificationsInterviewPYQsAI AssistantCareer
Start Learning Free🗺️ Learning Roadmaps

Python Foundations for AICheatsheets

Quick reference — commands, syntax, and patterns

✍️
Written by senior engineers. Reviewed for technical accuracy.· Updated 2025 · SynfraCore Python Foundations for AI Team
Expert Content

Python Foundations for AI — Quick Reference

JSON handling

python
import json

data = json.loads(response_text)       # string -> Python dict/list
text = json.dumps(data, indent=2)       # Python object -> pretty string
value = data.get("key", "default")      # safe access, no KeyError

Basic API call pattern

python
import os
from anthropic import Anthropic

client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=500,
    messages=[{"role": "user", "content": "Your prompt here"}],
)
print(response.content[0].text)

Streaming

python
with client.messages.stream(
    model="claude-sonnet-4-6", max_tokens=1000,
    messages=[{"role": "user", "content": prompt}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)   # flush=True matters, see Troubleshooting

Retry with exponential backoff

python
import time

def call_with_retry(fn, max_retries=3):
    for attempt in range(max_retries):
        try:
            return fn()
        except Exception:
            if attempt == max_retries - 1:
                raise
            time.sleep(2 ** attempt)

Cosine similarity (embeddings)

python
import numpy as np

def cosine_similarity(a: np.ndarray, b: np.ndarray) -> float:
    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

Async concurrent calls

python
import asyncio

async def call_many(prompts: list[str]) -> list[str]:
    return await asyncio.gather(*[call_one(p) for p in prompts])

Common error codes, quick reference

Code/ErrorUsual cause

|---|---|

`401`Missing/invalid API key
429Rate limited (requests or tokens per minute)
KeyError on responseAccessing a field that's conditionally present, or an error-response shape
nan from cosine similarityZero vector — usually a silently-failed embedding upstream
Async code hangsMissing await, or blocking sync call inside async function

Terminology quick reference

TermMeaning

|---|---|

EmbeddingA vector representation of text, used for semantic similarity
RAGRetrieval-Augmented Generation — retrieve relevant context, then generate an answer using it
Agent loopModel calls a tool, gets a result, continues — repeated until a final answer
Context windowThe maximum tokens (input + output) a model call can handle
Structured outputConstraining model output to a specific schema, instead of free text
Prompt cachingReusing billed context across calls instead of re-paying for repeated content
Share:
Join our Community
Daily tips, job alerts, interview help — join engineers learning together
Also Worth Exploring
← Back to all Python Foundations for AI modules
CertificationNotes