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

# Use LiteLLM with CometAPI

> Use this guide to configure LiteLLM with CometAPI by setting the base URL, API key, and model or provider options.

LiteLLM provides a unified Python API for 100+ LLM providers. CometAPI is natively supported — use the `cometapi/` prefix to route requests through CometAPI's model catalog.

## Prerequisites

* Python 3.6+
* A CometAPI account with an active API key — [get yours here](https://www.cometapi.com/console/token)

<Steps>
  <Step title="Install LiteLLM">
    ```bash theme={null}
    pip install litellm
    ```
  </Step>

  <Step title="Set your API key">
    Set the API key as an environment variable (recommended) or pass it inline:

    ```python theme={null}
    import os
    from litellm import completion

    # Recommended: environment variable
    os.environ["COMETAPI_KEY"] = "<COMETAPI_KEY>"

    # Alternative: pass inline
    api_key = "<COMETAPI_KEY>"
    ```

    <Note>
      Use environment variables to avoid hardcoding sensitive credentials in your scripts.
    </Note>
  </Step>

  <Step title="Make a completion call">
    Use the `cometapi/<model-name>` format to specify models. You can pass the key via environment variable or explicitly:

    ```python theme={null}
    messages = [{"content": "Hello, how are you?", "role": "user"}]

    # Method 1: environment variable (recommended)
    response = completion(model="cometapi/your-model-id", messages=messages)

    # Method 2: explicit API key
    response = completion(model="cometapi/your-model-id", messages=messages, api_key=api_key)

    print(response.choices[0].message.content)
    ```
  </Step>

  <Step title="Async and streaming calls">
    Use `acompletion` with `stream=True` for non-blocking, real-time responses:

    ```python theme={null}
    from litellm import acompletion
    import asyncio, traceback

    async def stream_call():
        try:
            response = await acompletion(
          model="cometapi/your-model-id",
                messages=[{"content": "Hello, how are you?", "role": "user"}],
                stream=True,
            )
            async for chunk in response:
                print(chunk)
        except Exception:
            print(f"Error: {traceback.format_exc()}")

    asyncio.run(stream_call())
    ```
  </Step>
</Steps>

<AccordionGroup>
  <Accordion title="Tips and troubleshooting">
    * **Model format**: CometAPI models use the prefix `cometapi/<model-name>`, e.g. `cometapi/your-model-id`. See the [CometAPI Models page](/overview/models) for available models.
    * **Fine-tuning responses**: LiteLLM supports `temperature`, `max_tokens`, and `top_p` — add them to any `completion()` call, e.g. `completion(..., temperature=0.7)`.
    * **Error handling**: Wrap calls in `try/except` to catch invalid key errors or network issues.
    * **Security**: Never commit API keys to version control. Use environment variables or a secrets manager.
    * **Rate limits**: Monitor usage in the [CometAPI console](https://www.cometapi.com/console).
    * **More docs**: [LiteLLM documentation](https://docs.litellm.ai/docs/) — [CometAPI quick start](/overview/quick-start)
  </Accordion>
</AccordionGroup>

<script type="application/ld+json">
  {`
    {
    "@context": "https://schema.org",
    "@graph": [
      {
        "@type": "HowTo",
        "@id": "https://apidoc.cometapi.com/integrations/litellm#howto",
        "name": "Use LiteLLM with CometAPI",
        "description": "Use this guide to configure LiteLLM with CometAPI by setting the base URL, API key, and model or provider options.",
        "step": [
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/litellm#step-1",
            "position": 1,
            "name": "Install LiteLLM",
            "text": "Complete the Install LiteLLM step in the Use LiteLLM with CometAPI guide."
          },
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/litellm#step-2",
            "position": 2,
            "name": "Set your API key",
            "text": "Store your CometAPI API key in the environment variable or settings field used by the integration."
          },
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/litellm#step-3",
            "position": 3,
            "name": "Make a completion call",
            "text": "Complete the Make a completion call step in the Use LiteLLM with CometAPI guide."
          },
          {
            "@type": "HowToStep",
            "@id": "https://apidoc.cometapi.com/integrations/litellm#step-4",
            "position": 4,
            "name": "Async and streaming calls",
            "text": "Complete the Async and streaming calls step in the Use LiteLLM with CometAPI guide."
          }
        ]
      },
      {
        "@type": "BreadcrumbList",
        "itemListElement": [
          {
            "@type": "ListItem",
            "position": 1,
            "name": "CometAPI Docs",
            "item": "https://apidoc.cometapi.com/"
          },
          {
            "@type": "ListItem",
            "position": 2,
            "name": "Integrations",
            "item": "https://apidoc.cometapi.com/integrations"
          },
          {
            "@type": "ListItem",
            "position": 3,
            "name": "Use LiteLLM with CometAPI",
            "item": "https://apidoc.cometapi.com/integrations/litellm"
          }
        ]
      }
    ]
    }
    `}
</script>
