> ## Documentation Index
> Fetch the complete documentation index at: https://docs.upsonic.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Task Result

> Accessing and managing task execution results and metadata

After task execution, you can access various aspects of the task results and metadata through the task object properties and methods.

## Basic Result Access

```python theme={null}
from upsonic import Agent, Task

# Create agent and execute task
agent = Agent(model="anthropic/claude-sonnet-4-5")
task = Task(description="What is the capital of France?")
agent.print_do(task)

# Access the response
print("Result:", task.response)
```

## Task Metadata

```python theme={null}
from upsonic import Agent, Task

# Create agent and execute task
agent = Agent(model="anthropic/claude-sonnet-4-5")
task = Task(description="Explain quantum computing in simple terms")
agent.print_do(task)

# Access task metadata
print(f"Task ID:        {task.task_id}")
print(f"Task Usage ID:  {task.task_usage_id}")
print(f"Start time:     {task.start_time}")
print(f"End time:       {task.end_time}")
if task.start_time and task.end_time:
    print(f"Wall-clock:     {(task.end_time - task.start_time):.2f}s")
```

## Cost Information

```python theme={null}
from upsonic import Agent, Task

# Create agent and execute task
agent = Agent(model="anthropic/claude-sonnet-4-5")
task = Task(description="Write a short poem about technology")
agent.print_do(task)

# Access cost information via the unified usage view
u = task.usage
if u.cost is not None:
    print(f"Total cost:   ${u.cost:.6f}")
print(f"Input tokens:  {u.input_tokens}")
print(f"Output tokens: {u.output_tokens}")
print(f"Requests:      {u.requests}")
```

## Tool Call History

```python theme={null}
from upsonic import Agent, Task
from upsonic.tools import tool

@tool
def get_weather(city: str) -> str:
    """Get weather for a city."""
    return f"Weather in {city}: Sunny, 25°C"

# Create agent and execute task with tools
agent = Agent(model="anthropic/claude-sonnet-4-5")
task = Task(description="What's the weather in Paris?", tools=[get_weather])
agent.print_do(task)

# Access tool call history
tool_calls = task.tool_calls
for i, tool_call in enumerate(tool_calls):
    print(f"Tool call {i+1}:")
    print(f"  Tool: {tool_call.get('tool_name')}")
    print(f"  Parameters: {tool_call.get('params')}")
    print(f"  Result: {tool_call.get('tool_result')}")
```

## Cache Information

```python theme={null}
from upsonic import Agent, Task

# Create agent and execute task with caching
agent = Agent(model="anthropic/claude-sonnet-4-5")
task = Task(
    description="What is machine learning?",
    enable_cache=True,
    cache_method="vector_search",
    cache_threshold=0.8
)
agent.print_do(task)

# Access cache statistics
cache_stats = task.get_cache_stats()
print(f"Cache hit: {cache_stats.get('cache_hit')}")
print(f"Cache method: {cache_stats.get('cache_method')}")
print(f"Cache threshold: {cache_stats.get('cache_threshold')}")
```

## Complete Example

```python theme={null}
from upsonic import Agent, Task
from pydantic import BaseModel

class ReportResult(BaseModel):
    title: str
    summary: str
    key_points: list[str]
    confidence: float

# Create and execute task
agent = Agent(model="anthropic/claude-sonnet-4-5", name="Analysis Agent")
task = Task(
    description="Generate a market analysis report",
    response_format=ReportResult,
    enable_cache=True
)

result = agent.print_do(task)

# Access all available information
print("=== TASK EXECUTION SUMMARY ===")
print(f"Task ID:        {task.get_task_id()}")
if task.start_time and task.end_time:
    print(f"Wall-clock:     {(task.end_time - task.start_time):.2f} seconds")
u = task.usage
if u.cost is not None:
    print(f"Cost:           ${u.cost:.6f}")
print(f"Tokens:         {u.input_tokens} in, {u.output_tokens} out")
print(f"Tool calls:     {len(task.tool_calls)}")
print(f"Cache hit:      {task.cache_hit}")

print("\n=== TASK RESULT ===")
print(f"Result: {result}")
print(f"Response type: {type(task.response)}")

print("\n=== CACHE STATISTICS ===")
cache_stats = task.get_cache_stats()
for key, value in cache_stats.items():
    print(f"{key}: {value}")
```

## Available Properties and Methods

| Property/Method         | Type              | Description                                                       |
| ----------------------- | ----------------- | ----------------------------------------------------------------- |
| **response**            | Any               | The task's response output                                        |
| **task\_id**            | str               | Unique task identifier                                            |
| **task\_usage\_id**     | str               | Scope tag for filtering the usage registry to this task           |
| **usage**               | `AggregatedUsage` | Read-only view of tokens / cost / requests / timing for this task |
| **start\_time**         | float \| None     | Unix timestamp when the task started                              |
| **end\_time**           | float \| None     | Unix timestamp when the task finished                             |
| **tool\_calls**         | List\[Dict]       | History of tool calls made                                        |
| **get\_cache\_stats()** | Dict              | Cache statistics and configuration                                |
| **get\_task\_id()**     | str               | Formatted task ID for display                                     |

## Best Practices

* **Result Validation**: Always check if results are None before processing
* **Error Handling**: Handle cases where metadata might not be available
* **Cost Monitoring**: Track costs for budget management
* **Performance Analysis**: Use duration metrics for optimization
* **Tool Debugging**: Review tool call history for debugging complex workflows
* **Cache Optimization**: Monitor cache hit rates to optimize caching strategies
