# Example queries
queries = [
    "I'm new to Python programming. Can you tell me about it?",
    "I got these test scores: 85, 92, 78, 96, 88. What's my average and how am I doing?",
    "I know some Python basics but want to learn machine learning. What should I do next?",
]

for i, query in enumerate(queries, 1):
    print(f"\n📝 Query {i}: {query}")
    result = Runner.run_sync(advanced_agent, query)
    print(f"🤖 Response: {result.final_output}")
    print("=" * 80)
```

### Adding granularity with the `@track` decorator

If you need more visibility into what happens inside your tool functions, you can use the `@track` decorator to trace specific steps within the tool execution:

```python
from agents import Agent, Runner, function_tool, set_trace_processors
from opik.integrations.openai.agents import OpikTracingProcessor
from opik import track

set_trace_processors(processors=[OpikTracingProcessor()])

@track(name="fetch_user_data")
def fetch_user_data(user_id: str) -> dict:
    # This step will be traced separately
    return {"user_id": user_id, "preferences": ["python", "ml"]}

@track(name="generate_recommendations")
def generate_recommendations(preferences: list) -> str:
    # This step will also be traced separately
    return f"Based on your interests in {', '.join(preferences)}, we recommend..."

@function_tool
def get_personalized_advice(user_id: str) -> str:
    """Get personalized learning advice for a user."""
    # Each tracked function call inside the tool will appear as a separate span
    user_data = fetch_user_data(user_id)
    recommendations = generate_recommendations(user_data["preferences"])
    return recommendations

agent = Agent(
    name="PersonalizedTutor",
    instructions="Help users with personalized learning advice.",
    model="gpt-4o-mini",
    tools=[get_personalized_advice]
)

result = Runner.run_sync(agent, "Give me learning advice for user_123")
print(result.final_output)
```

## Logging threads

When you are running multi-turn conversations with OpenAI Agents using [OpenAI Agents trace API](https://openai.github.io/openai-agents-python/running_agents/#conversationschat-threads), Opik integration automatically use the trace group\_id as the Thread ID so you can easily review conversation inside Opik. Here is an example below:

```python
async def main():
    agent = Agent(name="Assistant", instructions="Reply very concisely.")

    thread_id = str(uuid.uuid4())

    with trace(workflow_name="Conversation", group_id=thread_id):
        # First turn
        result = await Runner.run(agent, "What city is the Golden Gate Bridge in?")
        print(result.final_output)
        # San Francisco

        # Second turn
        new_input = result.to_input_list() + [{"role": "user", "content": "What state is it in?"}]
        result = await Runner.run(agent, new_input)
        print(result.final_output)
        # California
```

## Further improvements

OpenAI Agents is still a relatively new framework and we are working on a couple of improvements:

1. Improved rendering of the inputs and outputs for the LLM calls as part of our `Pretty Mode` functionality
2. Improving the naming conventions for spans
3. Adding the agent execution input and output at a trace level

If there are any additional improvements you would like us to make, feel free to open an issue on our [GitHub repository](https://github.com/comet-ml/opik/issues).