# Use start_as_current_span to track workflow execution
with opik.start_as_current_span(
    name="rag_workflow_execution",
    input={"query": "What are the key features?"},
    project_name="llama-index-workflows"
) as span:
    result = await workflow.run(query="What are the key features?")
    span.update(output={"result": result})
    
print(result)
opik.flush_tracker()  # Ensure all traces are sent
```

In this example:

* Each workflow step is decorated with `@opik.track()` to create spans
* The `@step` decorator is placed before `@opik.track()` to ensure LlamaIndex can properly discover the workflow steps
* `opik.start_as_current_span()` tracks the overall workflow execution
* LLM calls within steps are automatically tracked via the global Opik handler
* All workflow steps appear as nested spans within the workflow trace

<Tip>
  If you're certain the workflow is a top-level call and want to create only a trace without an additional span, you can use `opik.start_as_current_trace()` instead of `opik.start_as_current_span()`. However, `start_as_current_span()` is more flexible as it works in both standalone and nested contexts.
</Tip>

### Best Practices

1. **Decorate all workflow steps** with `@opik.track()` to capture each step as a span
2. **Decorator order matters**: Place `@step` before `@opik.track()` so LlamaIndex's workflow engine can properly discover and execute steps
3. **Use `opik.start_as_current_span()`** to wrap workflow execution - it works in both standalone and nested contexts
4. **Configure the global handler** to automatically track LLM calls within steps
5. **Use descriptive names** for spans to make debugging easier
6. **Always call `opik.flush_tracker()`** at the end to ensure all traces are sent
7. **Include input/output** in span updates for better debugging

## Token Usage in Streaming Responses

When using streaming chat responses with OpenAI models (e.g., `llm.stream_chat()`), you need to explicitly enable token usage tracking by configuring the `stream_options` parameter:

```python
from llama_index.llms.openai import OpenAI
from llama_index.core.llms import ChatMessage
from llama_index.core import global_handler, set_global_handler