Skip to main content
GACS will never ask for your seed phrase, private keys, or payment. free and ad-free.
Back to lessonLab · Module 10 / 12
Lab

Scaling & Observability

Hands-on lab with starter code you can copy + run, and guided steps to produce the artifact for this module.

Setup

  1. Open a fresh terminal in a new folder, e.g. mkdir lab-10 && cd lab-10.
  2. Create a Python 3.11 venv: python -m venv .venv && source .venv/bin/activate.
  3. Install the dependencies noted on each starter snippet (the small pip install … line above the code).
  4. Copy each starter into a file with the suggested name, then run it and capture the output.

Starter code

01 · Trace every step with OpenTelemetry
pip install opentelemetry-sdk opentelemetry-exporter-otlp
save as: 10_01_lab.py
python
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter

trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))
tracer = trace.get_tracer("agent")

def traced_step(name, fn, *a, **kw):
    with tracer.start_as_current_span(name) as span:
        span.set_attribute("input", str(a)[:200])
        out = fn(*a, **kw)
        span.set_attribute("output", str(out)[:200])
        return out
Run with: python 10_01_lab.py
02 · Token + cost budget guard
save as: 10_02_lab.py
python
class Budget:
    def __init__(self, max_usd):
        self.spent = 0.0; self.max = max_usd
    def charge(self, in_tok, out_tok, in_price, out_price):
        cost = in_tok*in_price/1000 + out_tok*out_price/1000
        self.spent += cost
        if self.spent > self.max:
            raise RuntimeError(f"budget exceeded: {self.spent:.4f} > {self.max}")
        return cost
Run with: python 10_02_lab.py

Guided steps

Work through each step in order. Stop when your output matches the expected artifact — then push your work to your lab folder.

  1. 1
    OTel tracing

    Do — Wrap every agent step in an OTel span with input/output attributes. Export to console.

    Expected artifact — Console shows nested spans for each /ask call with non-empty attributes.

    Run free:
  2. 2
    Budget guard

    Do — Implement a per-request USD budget. Cap one request at $0.001 and confirm it raises.

    Expected artifact — RuntimeError: budget exceeded; cost logged.

    Run free:
  3. 3
    Load test

    Do — Use hey or k6 to send 100 RPS for 30s. Record p50/p95 and error rate.

    Expected artifact — load.md with the latency table and a 2-line tuning recommendation.

    Run free:

Authoritative sources

Independent primary sources used to check and corroborate the guidance on this page.