Documentation Index
Fetch the complete documentation index at: https://mintlify.com/sidmanale643/northstar/llms.txt
Use this file to discover all available pages before exploring further.
northstar.model_call() is a context manager that opens a SpanKind.MODEL span around an LLM provider call and yields a ModelSpan handle you use to record the conversation, token usage, and cost. It is the recommended way to manually instrument LLM calls when auto-instrumentation via auto_instrument() is not available or not desired. All four methods on ModelSpan are safe to call in any order; the span is finalized — with latency and error status — when the with block exits.
Function signature
Parameters
Display name for the model span shown in the trace waterfall. Use a descriptive label for the role this LLM call plays, e.g.
"answer-llm", "summarizer", or "classify-intent".The model identifier passed to the provider, e.g.
"gpt-4o", "claude-sonnet-4-5-20250929", "gemini-1.5-pro". This string is stored in span.attributes["model"] and used by the pricing module to look up per-token USD rates when record_usage() is called.Attach this model span to a specific
Run object instead of the currently active trace. Useful in low-level code that holds a Run reference directly. When None (the default), the span is attached to the active trace detected from the ContextVar.ModelSpan methods
The context manager yields a ModelSpan instance. All methods delegate to the underlying Span record and enqueue updates to the SDK’s background worker.
Records the list of messages sent to the model. Each message dict is parsed by role (
"system", "user", "assistant", "tool") and stored as the corresponding event type. Returns the estimated prompt token count for the model (computed via the litellm pricing module; requires the pricing extra).Records the message returned by the model. Pass the response message dict (e.g.
response.choices[0].message.model_dump()). Returns the estimated completion token count. If input tokens were already recorded via record_input_messages(), the method also computes and stores the estimated USD cost in span.attributes["cost_usd"].Records exact token counts reported by the provider (from the API response’s
usage object) and computes the USD cost. Overwrites any estimates written by record_input_messages() / record_output_message(). Returns the computed USD cost as a float, or None if the model is not found in the pricing database.prompt_tokens(int, required) — tokens consumed by the prompt.completion_tokens(int, required) — tokens generated by the model.source(str, default"litellm") — pricing source label stored inspan.attributes["pricing_source"].
The UUID of the underlying
Span record. None when the SDK is in no-op mode.The model identifier passed to
model_call(). Empty string in no-op mode.Full usage example
Multi-turn example
No-op behavior
When there is no active trace, or when the SDK is disabled,model_call() yields a _NoopModelSpan. All methods on _NoopModelSpan accept the same arguments and silently return 0 (for token counts) or None (for cost), so your code path does not need to branch.
Cost aggregation
After the run finishes, NorthStar automatically aggregatescost_usd, input_tokens, and output_tokens across all MODEL-kind spans and writes totals to the run’s metadata (cost_usd, total_input_tokens, total_output_tokens). These totals appear in the dashboard’s run summary view.
Relationship to auto_instrument()
northstar.auto_instrument() uses model_call() internally. When auto-instrumentation is active for OpenAI or Anthropic, every provider call is automatically wrapped in a model_call() span — you do not need to call it manually. Use model_call() directly when you use a provider not yet covered by auto-instrumentation, or when you need fine-grained control over what is recorded.
Install the
pricing extra (uv add 'northstar-ai[pricing]') to enable
token counting and USD cost computation. Without it, record_usage() still
stores the raw token counts but returns None for cost, and
record_input_messages() / record_output_message() return 0.