AgentOpsAgentOps 0.4.21 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
11 small wins to finish your pathNext lesson

LLM calls: prompts, completions and tokens

init patches installed LLM libraries. An OpenAI call becomes a span with the model, the prompt, the completion and token counts, without any decorator.

Examplellm.py, after the setup lines
from openai import OpenAI

client = OpenAI(base_url=f"{url}/v1", api_key="not-a-real-key")


@agentops.trace(name="support-ticket")
def handle(ticket):
    reply = client.chat.completions.create(model="gpt-4.1-mini", messages=[{"role": "user", "content": ticket}])
    return reply.choices[0].message.content
Examplellm.py, after the setup lines, continued
print(handle("I was charged twice for order A-1001"))
local_collector.flush()
llm = next(span for span in local_collector.SPANS if span["name"] == "openai.chat.completion")
for key, value in llm["attributes"].items():
    if key.startswith("gen_ai."):
        print(key, "=", value)
Example
python llm.py

The OpenAI client is pointed at the collector's fake API with base_url, so no request goes to OpenAI and the key is a placeholder. AgentOps recorded the call as openai.chat.completion inside the trace: gen_ai.system, the requested and returned model, the prompt messages as gen_ai.prompt.N, the answer as gen_ai.completion.N, and token usage from the response. These names follow OpenTelemetry's gen_ai conventions.

Leaving prompt text out

Example
python llm_no_content.py

With TRACELOOP_TRACE_CONTENT=false set before AgentOps is imported, the prompt and completion text are not recorded, while the model and token counts still are. The variable's name comes from OpenLLMetry, the instrumentation AgentOps builds on. It only covers LLM calls: decorator inputs, lesson 4, are still recorded.

Try it yourself
  • Send two messages, a system and a user message, and print the prompt attributes.
  • Set the variable after import agentops and check whether it still works.
  • Stream the completion with stream=True and compare the span.

Every expert started right here.