> For the complete documentation index, see [llms.txt](https://docs.projectbit.ca/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.projectbit.ca/documentation/agents/reasoning.md).

# Reasoning

reasoning\_agent.py

```python
from bitca.agent import Agent
from bitca.model.openai import OpenAIChat

task = (
    "Three missionaries and three cannibals need to cross a river. "
    "They have a boat that can carry up to two people at a time. "
    "If, at any time, the cannibals outnumber the missionaries on either side of the river, the cannibals will eat the missionaries. "
    "How can all six people get across the river safely? Provide a step-by-step solution and show the solutions as an ascii diagram"
)

reasoning_agent = Agent(model=OpenAIChat(id="gpt-4o"), reasoning=True, markdown=True, structured_outputs=True)
reasoning_agent.print_response(task, stream=True, show_full_reasoning=True)
```

Run the Reasoning Agent:

```bash
pip install -U bitca openai

export OPENAI_API_KEY=***

python reasoning_agent.py
```

### How to use reasoning <a href="#how-to-use-reasoning" id="how-to-use-reasoning"></a>

To add reasoning, set `reasoning=True`. When using reasoning with tools, do not use `structured_outputs=True` as gpt-4o cannot use tools with structured outputs.

```python
reasoning_agent = Agent(
    model=OpenAIChat(id="gpt-4o-2024-08-06"),
    reasoning=True,
    markdown=True,
    structured_outputs=True,
)
reasoning_agent.print_response("How many 'r' are in the word 'supercalifragilisticex
```

### Reasoning with tools <a href="#reasoning-with-tools" id="reasoning-with-tools"></a>

You can also use tools with a reasoning agent, but do not use `structured_outputs=True` as gpt-4o cannot use tools with structured outputs. Lets create a finance agent that can reason.

finance\_reasoning.py

```python
from bitca.agent import Agent
from bitca.model.openai import OpenAIChat
from bitca.tools.yfinance import YFinanceTools

reasoning_agent = Agent(
    model=OpenAIChat(id="gpt-4o"),
    tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True, company_news=True)],
    instructions=["Use tables to show data"],
    show_tool_calls=True,
    markdown=True,
    reasoning=True,
)
reasoning_agent.print_response("Write a report comparing NVDA to TSLA", stream=True, show_full_reasoning=True)
```

Run the script to see the output.

```bash
pip install -U bitca openai yfinance

export OPENAI_API_KEY=***

python finance_reasoning.py
```

### More reasoning examples <a href="#more-reasoning-examples" id="more-reasoning-examples"></a>

#### [​](https://docs.phidata.com/agents/reasoning#logical-puzzles)Logical puzzles <a href="#logical-puzzles" id="logical-puzzles"></a>

logical\_puzzle.py

```python
from bitca.agent import Agent
from bitca.model.openai import OpenAIChat

task = (
    "Three missionaries and three cannibals need to cross a river. "
    "They have a boat that can carry up to two people at a time. "
    "If, at any time, the cannibals outnumber the missionaries on either side of the river, the cannibals will eat the missionaries. "
    "How can all six people get across the river safely? Provide a step-by-step solution and show the solutions as an ascii diagram"
)
reasoning_agent = Agent(
    model=OpenAIChat(id="gpt-4o-2024-08-06"), reasoning=True, markdown=True, structured_outputs=True
)
reasoning_agent.print_response(task, stream=True, show_full_reasoning=True)
```

Run the script to see the output.

```bash
pip install -U bitca openai

export OPENAI_API_KEY=***

python logical_puzzle.py
```

#### [​](https://docs.phidata.com/agents/reasoning#mathematical-proofs)Mathematical proofs <a href="#mathematical-proofs" id="mathematical-proofs"></a>

mathematical\_proof.py

```python
from bitca.agent import Agent
from bitca.model.openai import OpenAIChat

task = "Prove that for any positive integer n, the sum of the first n odd numbers is equal to n squared. Provide a detailed proof."
reasoning_agent = Agent(
    model=OpenAIChat(id="gpt-4o-2024-08-06"), reasoning=True, markdown=True, structured_outputs=True
)
reasoning_agent.print_response(task, stream=True, show_full_reasoning=True)
```

Run the script to see the output.

```bash
pip install -U bitca openai

export OPENAI_API_KEY=***

python mathematical_proof.py
```

#### [​](https://docs.phidata.com/agents/reasoning#scientific-research)Scientific research <a href="#scientific-research" id="scientific-research"></a>

scientific\_research.py

```python
from bitca.agent import Agent
from bitca.model.openai import OpenAIChat

task = (
    "Read the following abstract of a scientific paper and provide a critical evaluation of its methodology,"
    "results, conclusions, and any potential biases or flaws:\n\n"
    "Abstract: This study examines the effect of a new teaching method on student performance in mathematics. "
    "A sample of 30 students was selected from a single school and taught using the new method over one semester. "
    "The results showed a 15% increase in test scores compared to the previous semester. "
    "The study concludes that the new teaching method is effective in improving mathematical performance among high school students."
)
reasoning_agent = Agent(
    model=OpenAIChat(id="gpt-4o-2024-08-06"), reasoning=True, markdown=True, structured_outputs=True
)
reasoning_agent.print_response(task, stream=True, show_full_reasoning=True)
```

Run the script to see the output.

```bash
pip install -U bitca openai

export OPENAI_API_KEY=***

python scientific_research.py
```

#### [​](https://docs.phidata.com/agents/reasoning#ethical-dilemma)Ethical dilemma <a href="#ethical-dilemma" id="ethical-dilemma"></a>

ethical\_dilemma.py

```python
from bitca.agent import Agent
from bitca.model.openai import OpenAIChat

task = (
    "You are a train conductor faced with an emergency: the brakes have failed, and the train is heading towards "
    "five people tied on the track. You can divert the train onto another track, but there is one person tied there. "
    "Do you divert the train, sacrificing one to save five? Provide a well-reasoned answer considering utilitarian "
    "and deontological ethical frameworks. "
    "Provide your answer also as an ascii art diagram."
)
reasoning_agent = Agent(
    model=OpenAIChat(id="gpt-4o-2024-08-06"), reasoning=True, markdown=True, structured_outputs=True
)
reasoning_agent.print_response(task, stream=True, show_full_reasoning=True)
```

Run the script to see the output.

```bash
pip install -U bitca openai

export OPENAI_API_KEY=***

python ethical_dilemma.py
```

#### [​](https://docs.phidata.com/agents/reasoning#planning-an-itinerary)Planning an itinerary <a href="#planning-an-itinerary" id="planning-an-itinerary"></a>

planning\_itinerary.py

```python
from bitca.agent import Agent
from bitca.model.openai import OpenAIChat

task = "Plan an itinerary from Los Angeles to Las Vegas"
reasoning_agent = Agent(
    model=OpenAIChat(id="gpt-4o-2024-08-06"), reasoning=True, markdown=True, structured_outputs=True
)
reasoning_agent.print_response(task, stream=True, show_full_reasoning=True)
```

Run the script to see the output.

```bash
pip install -U bitca openai

export OPENAI_API_KEY=***

python planning_itinerary.py
```

#### [​](https://docs.phidata.com/agents/reasoning#creative-writing)Creative writing <a href="#creative-writing" id="creative-writing"></a>

creative\_writing.py

```python
from bitca.agent import Agent
from bitca.model.openai import OpenAIChat

task = "Write a short story about life in 5000000 years"
reasoning_agent = Agent(
    model=OpenAIChat(id="gpt-4o-2024-08-06"), reasoning=True, markdown=True, structured_outputs=True
)
reasoning_agent.print_response(task, stream=True, show_full_reasoning=True)
```

Run the script to see the output.

```bash
pip install -U bitca openai

export OPENAI_API_KEY=***

python creative_writing.py
```
