Besides the information that is already automatically captured once you integrate with Openlayer, you might want to add additional metadata to your traces. This guide shows you how to do it.
Want to add user and session information to your traces? Check out the Sessions and users guide instead.

Adding trace-level metadata

Use the function update_current_trace() from Openlayer’s Python SDK to attach metadata to the entire trace.
from openlayer.lib import trace, update_current_trace

@trace()
def my_function(scenario_type: str) -> str:
    # Add metadata to the entire trace
    update_current_trace(
        scenario_type=scenario_type,
        model_version="0.1.0"
    )

    # Your application logic here
    return "Some answer"
All key-value pairs passed to update_current_trace() will be stored in the trace metadata and visible and filterable in the Openlayer platform.

Adding step-level metadata

Use update_current_step() to attach metadata to individual steps within a trace. This is useful for step-specific information like intermediate results or processing details.
from openlayer.lib import trace, update_current_step

@trace()
def retrieve_context(query: str) -> str:
    # Perform retrieval
    results = vector_search(query, top_k=5)

    # Add metadata about this retrieval step
    update_current_step(
        metadata={
            "retrieval_method": "vector_search",
            "top_k": 5,
            "num_results_found": len(results),
        }
    )

    return format_context(results)

@trace()
def generate_answer(query: str, context: str) -> str:
    response = openai_client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": f"{query}\n\nContext: {context}"}],
        temperature=0.7
    )

    # Add generation-specific metadata
    update_current_step(
        metadata={
            "context_length": len(context),
            "response_length": len(response.choices[0].message.content)
        }
    )

    return response.choices[0].message.content