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

# Ollama

> AgentOps provides first class support for Ollama

[Ollama](https://ollama.com) is a lightweight, open-source tool for running and managing LLM models. Track your Ollama model calls with AgentOps.

<Steps>
  <Step title="Install the AgentOps SDK">
    <CodeGroup>
      ```bash pip  theme={null}
      pip install agentops ollama
      ```

      ```bash poetry theme={null}
      poetry add agentops ollama
      ```
    </CodeGroup>
  </Step>

  <Step title="Install the Ollama SDK">
    <CodeGroup>
      ```bash pip theme={null}
      pip install ollama
      ```

      ```bash poetry theme={null}
      poetry add ollama
      ```
    </CodeGroup>
  </Step>

  <Step title="Add 3 lines of code">
    <Note>
      Make sure to call `agentops.init` before calling any `openai`, `cohere`, `crew`, etc models.
    </Note>

    <CodeGroup>
      ```python python theme={null}
      import agentops
      import ollama

      agentops.init(<INSERT YOUR API KEY HERE>)
      agentops.start_session()

      ollama.pull("<MODEL NAME>")

      response = ollama.chat(model='mistral',
        messages=[{
            'role': 'user',
            'content': 'What are the benefits of using AgentOps for monitoring LLMs?',
        }]
      )
      print(response['message']['content'])
      ...
      # End of program (e.g. main.py)
      agentops.end_session("Success")
      ```
    </CodeGroup>

    <Tip>
      Set your API key as an `.env` variable for easy access.
    </Tip>

    <CodeGroup>
      ```python .env theme={null}
      # Alternatively, you can set the API key as an environment variable
      AGENTOPS_API_KEY=<YOUR API KEY>
      ```
    </CodeGroup>

    Read more about environment variables in [Advanced Configuration](/v1/usage/advanced-configuration)
  </Step>

  <Step title="Run your Agent">
    Execute your program and visit [app.agentops.ai/drilldown](https://app.agentops.ai/drilldown) to observe your Agent! 🕵️

    <Tip>
      After your run, AgentOps prints a clickable url to console linking directly to your session in the Dashboard
    </Tip>

    <div />
  </Step>
</Steps>

## Full Examples

<CodeGroup>
  ```python basic completion theme={null}
  import ollama
  import agentops

  agentops.init(<INSERT YOUR API KEY HERE>)

  ollama.pull("<MODEL NAME>")
  response = ollama.chat(
      model="<MODEL NAME>",
      max_tokens=1024,
      messages=[{
          "role": "user",
          "content": "Write a haiku about AI and humans working together"
      }]
  )

  print(response['message']['content'])
  agentops.end_session('Success')
  ```

  ```python streaming theme={null}
  import agentops
  import ollama

  async def main():
      agentops.init(<INSERT YOUR API KEY HERE>)
      ollama.pull("<MODEL NAME>")

      stream = ollama.chat(
        model="<MODEL NAME>",
          messages=[{
              'role': 'user',
              'content': 'Write a haiku about monitoring AI agents',
          }], 
          stream=True
      )

      for chunk in stream:
          print(chunk['message']['content'], end='')

      agentops.end_session('Success')
  ```

  ```python conversation theme={null}
  import ollama
  import agentops

  agentops.init(<INSERT YOUR API KEY HERE>)
  ollama.pull("<MODEL NAME>")

  messages = [
    {
        'role': 'user',
        'content': 'What is AgentOps?'
    },
    {
        'role': 'assistant',
        'content': 'AgentOps is a monitoring and observability platform for LLM applications.'
    },
    {
        'role': 'user',
        'content': 'Can you give me 3 key features?'
    }
  ]

  response = ollama.chat(
      model="<MODEL NAME>",
      messages=messages
  )
  print(response['message']['content'])   
  agentops.end_session('Success')
  ```
</CodeGroup>

<script type="module" src="/scripts/github_stars.js" />

<script type="module" src="/scripts/scroll-img-fadein-animation.js" />

<script type="module" src="/scripts/button_heartbeat_animation.js" />

<script type="css" src="/styles/styles.css" />
