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

Basic Result Access

from upsonic import Agent, Task

# Create agent and execute task
agent = Agent(model="openai/gpt-4o")
task = Task(description="What is the capital of France?")
result = agent.do(task)

# Access the response
print(f"Task result: {result}")
print(f"Task response: {task.response}")

Task Metadata

from upsonic import Agent, Task

# Create agent and execute task
agent = Agent(model="openai/gpt-4o")
task = Task(description="Explain quantum computing in simple terms")
result = agent.do(task)

# Access task metadata
print(f"Task ID: {task.task_id}")
print(f"Price ID: {task.price_id}")
print(f"Duration: {task.duration}")
print(f"Start time: {task.start_time}")
print(f"End time: {task.end_time}")

Cost Information

from upsonic import Agent, Task

# Create agent and execute task
agent = Agent(model="openai/gpt-4o")
task = Task(description="Write a short poem about technology")
result = agent.do(task)

# Access cost information
total_cost = task.total_cost
input_tokens = task.total_input_token
output_tokens = task.total_output_token

print(f"Total cost: ${total_cost}")
print(f"Input tokens: {input_tokens}")
print(f"Output tokens: {output_tokens}")

Tool Call History

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="openai/gpt-4o")
task = Task(description="What's the weather in Paris?", tools=[get_weather])
result = agent.do(task)
print(result)

# 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

from upsonic import Agent, Task

# Create agent and execute task with caching
agent = Agent(model="openai/gpt-4o")
task = Task(
    description="What is machine learning?",
    enable_cache=True,
    cache_method="vector_search",
    cache_threshold=0.8
)
result = agent.do(task)
print(result)

# 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

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="openai/gpt-4o", name="Analysis Agent")
task = Task(
    description="Generate a market analysis report",
    response_format=ReportResult,
    enable_cache=True
)

result = agent.do(task)

# Access all available information
print("=== TASK EXECUTION SUMMARY ===")
print(f"Task ID: {task.get_task_id()}")
print(f"Duration: {task.duration:.2f} seconds")
print(f"Cost: ${task.total_cost}")
print(f"Tokens: {task.total_input_token} in, {task.total_output_token} out")
print(f"Tool calls made: {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/MethodTypeDescription
responseAnyThe task’s response output
task_idstrUnique task identifier
price_idstrPrice tracking identifier
durationfloatTask execution duration in seconds
total_costfloatEstimated cost in USD
total_input_tokenintNumber of input tokens used
total_output_tokenintNumber of output tokens generated
tool_callsList[Dict]History of tool calls made
get_cache_stats()DictCache statistics and configuration
get_task_id()strFormatted 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