> ## 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.

# Adding Tasks to Other Tasks as Context

> Using task outputs as context for other tasks in the Upsonic framework

In Upsonic, you can use the output of one task as context for another task through the `context` parameter. This enables task chaining and complex workflows.

## Basic Task Chaining

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

agent = Agent(model="anthropic/claude-sonnet-4-5")

# First task
task1 = Task(description="What is 2 + 2?")
result1 = agent.print_do(task1)
print(f"Result 1: {result1}")

# Second task using context from first task
task2 = Task(
    description="Based on the previous result, what is that number multiplied by 3?",
    context=[task1]
)
result2 = agent.print_do(task2)
print(f"Result 2: {result2}")
```

## Multiple Context Sources

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

agent = Agent(model="anthropic/claude-sonnet-4-5")

# First two tasks
task1 = Task(description="What is 10 + 5?")
result1 = agent.print_do(task1)

task2 = Task(description="What is 20 - 8?", context=[task1])
result2 = agent.print_do(task2)

# Task with multiple context sources
task3 = Task(
    description="Summarize all the previous mathematical results",
    context=[task1, task2, "Additional context: These were all math problems"]
)
result3 = agent.print_do(task3)
print(result3)
```

## Complex Workflow Example

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

@tool
def data_collector(source: str) -> str:
    """Collect data from a source."""
    return f"Collected data from {source}"

@tool
def data_analyzer(data: str) -> str:
    """Analyze data."""
    return f"Analysis of: {data}"

class ReportResult(BaseModel):
    title: str
    summary: str
    insights: List[str]

agent = Agent(model="anthropic/claude-sonnet-4-5")

# Task 1: Data collection
task1 = Task(
    description="Collect data from market research sources",
    tools=[data_collector],
    enable_cache=True
)
result1 = agent.print_do(task1)

# Task 2: Data analysis
task2 = Task(
    description="Analyze the collected market data",
    tools=[data_analyzer],
    context=[task1],
    enable_thinking_tool=True
)
result2 = agent.print_do(task2)

# Task 3: Report generation
task3 = Task(
    description="Generate a comprehensive market report",
    context=[task1, task2],
    response_format=ReportResult
)
result3 = agent.print_do(task3)
print(f"Report: {result3.title}")
print(f"Summary: {result3.summary}")
```

## Context Types

Tasks can use various types of context:

* **Other Tasks**: Previous task results for chaining
* **Strings**: Direct text context
* **Knowledge Bases**: RAG-enabled knowledge sources
* **Mixed**: Combination of different context types

```python theme={null}
from upsonic import Agent, Task, KnowledgeBase
from upsonic.embeddings import OpenAIEmbedding
from upsonic.vectordb import ChromaProvider, ChromaConfig, ConnectionConfig, Mode

# Setup knowledge base
embedding_provider = OpenAIEmbedding()
vectordb = ChromaProvider(config=ChromaConfig(collection_name="kb", vector_size=1536, connection=ConnectionConfig(mode=Mode.IN_MEMORY)))
knowledge_base = KnowledgeBase(sources=["company_data.txt"], embedding_provider=embedding_provider, vectordb=vectordb)

agent = Agent(model="anthropic/claude-sonnet-4-5")

# Previous data collection task
data_collection_task = Task(description="Collect Q4 sales data")
agent.do(data_collection_task)

# Mixed context example
task = Task(
    description="Analyze the data and provide insights",
    context=[
        data_collection_task,  # Previous task
        "Focus on Q4 performance",  # String context
        knowledge_base  # Knowledge base context
    ]
)
result = agent.print_do(task)
print(result)
```

## Best Practices

* **Logical Flow**: Design task chains with clear logical progression
* **Context Relevance**: Only include context that's relevant to the current task
* **Performance**: Consider caching for expensive context processing
* **Error Handling**: Handle cases where context tasks might fail
* **Documentation**: Clearly document the expected context flow in your workflows
