Adding Metadata
You can add additional information to spans using metadata and internals. Both use semantic conventions under the ag namespace. Metadata is saved under ag.meta. Internals are saved under ag.data.internals.
See the semantic conventions guide for more details on how attributes are organized.
Adding metadata
Use ag.tracing.store_meta() to add metadata to a span. This function accesses the active span from the context and adds the key-value pairs to the metadata.
@ag.instrument(spankind="task")
def compile_prompt(country: str):
prompt = f"What is the capital of {country}"
ag.tracing.store_meta({"prompt_template": prompt})
formatted_prompt = prompt.format(country=country)
return formatted_prompt
Storing internals
Use ag.tracing.store_internals() to store internals in a span:
@ag.instrument(spankind="workflow")
def rag_workflow(query: str):
context = retrieve_context(query)
ag.tracing.store_internals({"context": context})
prompt = f"Answer the following question {query} based on the context: {context}"
completion = client.chat.completions.create(
model='gpt-4',
messages=[
{'role': 'user', 'content': prompt},
],
)
return completion.choices[0].message.content
Differences between metadata and internals
Both metadata and internals can be used for evaluation and filtering. The main differences are:
- Internals are searchable using plain text queries because they are saved under
ag.data. - Internals are shown in the overview tab of the observability drawer together with inputs and outputs, making them easy to see.
As a rule of thumb, if your context is short, put important information that helps understand the span into internals.
Next steps
- Link traces to prompt versions
- Understand how to redact sensitive data
- Explore distributed tracing across services
