Getting started overview

This guide provides a focused walkthrough for developers to initiate usage of the GroqCloud API, covering account creation, API key retrieval, and making a foundational API call. Groq specializes in low-latency large language model (LLM) inference through its Language Processing Unit (LPU) Inference Engine. The GroqCloud API offers a programmatic interface to these capabilities, designed for real-time applications requiring rapid response times.

The API is designed for compatibility with the OpenAI API specification, aiming to simplify integration for developers already familiar with similar LLM interfaces. This approach reduces the learning curve and allows for quicker adoption for those transitioning or extending their AI applications to Groq's infrastructure Groq developer documentation.

Quick reference steps

The following table summarizes the essential steps to get started with the GroqCloud API:

Step What to do Where
1. Sign Up Create a GroqCloud account. Groq homepage
2. Generate API Key Access the API Keys section in your Groq console and create a new key. GroqCloud Console > API Keys
3. Install SDK (Optional) Install the official Python or JavaScript SDK. pip install groq or npm install groq
4. Make First Request Use your API key to send a chat completion request to the API. Your preferred development environment
5. Monitor Usage Review your API usage and costs. GroqCloud Console > Usage

Create an account and get keys

Access to the GroqCloud API begins with account registration and the generation of an API key. This key authenticates your requests and links them to your usage metrics and billing.

Account registration

  1. Navigate to the Groq official website.
  2. Locate and select the 'Sign Up' or 'Get Started' option.
  3. Complete the registration form, providing necessary details such as email and password.
  4. Verify your email address if prompted, which is a standard security measure for new accounts.

API key generation

Once your account is active:

  1. Log in to your GroqCloud console.
  2. From the dashboard, navigate to the 'API Keys' section. The exact path may vary but is typically found under 'Settings' or 'Developer' options.
  3. Click on 'Create new API Key' or a similar prompt.
  4. Assign a descriptive name to your API key (e.g., "My First App Key"). This helps with organization, especially if you manage multiple applications.
  5. The newly generated API key will be displayed. Copy this key immediately and store it securely. For security reasons, Groq will not display the full key again after it's generated. Best practices dictate against hardcoding API keys directly into source code; instead, use environment variables or a secure configuration management system Google Cloud API key best practices.

Your first request

After obtaining your API key, you can make your first call to the GroqCloud API. This example demonstrates a basic chat completion request, which is a common use case for LLMs. Groq supports several models; this example uses LLaMA3 8B, which is a suitable model for initial testing.

Prerequisites

  • Your Groq API key.
  • A development environment with Python or Node.js installed.
  • The Groq Python or JavaScript SDK installed (optional, but recommended for ease of use).

Python example

First, install the Python SDK:

pip install groq

Then, create a Python file (e.g., groq_first_call.py) with the following content:

from groq import Groq
import os

client = Groq(
    api_key=os.environ.get("GROQ_API_KEY"),
)

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Explain the concept of low-latency LLM inference in one sentence.",
        }
    ],
    model="llama3-8b-8192", # Specify the model
    temperature=0.7,
    max_tokens=100,
    top_p=1,
    stop=None,
    stream=False,
)

print(chat_completion.choices[0].message.content)

Before running, set your API key as an environment variable:

export GROQ_API_KEY='YOUR_GROQ_API_KEY'

Then run the script:

python groq_first_call.py

JavaScript example

First, install the JavaScript SDK:

npm install groq

Then, create a JavaScript file (e.g., groqFirstCall.js) with the following content:

import Groq from "groq";

const groq = new Groq({
  apiKey: process.env.GROQ_API_KEY,
});

async function getGroqChatCompletion() {
  return groq.chat.completions.create({
    messages: [
      {
        role: "user",
        content: "Explain the concept of low-latency LLM inference in one sentence.",
      },
    ],
    model: "llama3-8b-8192", // Specify the model
    temperature: 0.7,
    max_tokens: 100,
    top_p: 1,
    stop: null,
    stream: false,
  });
}

getGroqChatCompletion().then((chatCompletion) => {
  console.log(chatCompletion.choices[0]?.message?.content || "No content received");
}).catch(error => {
  console.error("Error making Groq API call:", error);
});

Before running, set your API key as an environment variable:

export GROQ_API_KEY='YOUR_GROQ_API_KEY'

Then run the script:

node groqFirstCall.js

cURL example

For direct HTTP requests, you can use curl:

curl -X POST https://api.groq.com/openai/v1/chat/completions \
  -H "Authorization: Bearer $GROQ_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "messages": [ { "role": "user", "content": "Explain the concept of low-latency LLM inference in one sentence." } ], "model": "llama3-8b-8192", "temperature": 0.7, "max_tokens": 100, "top_p": 1, "stream": false }'

Common next steps

After successfully making your first request, consider these common next steps to further integrate and optimize your use of the GroqCloud API:

  • Explore other models: Groq supports various LLMs, each with different performance characteristics. Refer to the Groq models documentation to understand available options and their specific capabilities.
  • Implement streaming: For real-time applications, consider using the streaming API to receive tokens as they are generated, rather than waiting for the entire response. This is particularly beneficial for conversational AI interfaces.
  • Integrate into an application: Move beyond standalone scripts and integrate the API calls into a larger application, ensuring proper error handling, retry mechanisms, and robust security practices for API key management.
  • Monitor usage and costs: Regularly check your usage statistics and billing in the GroqCloud console. Groq's pricing structure is pay-as-you-go, based on token consumption.
  • Review rate limits: Understand the rate limits applicable to your plan to prevent unexpected interruptions in service. Information on Groq rate limits is available in the documentation.
  • Advanced API features: Investigate features like function calling, which allows LLMs to interact with external tools and APIs, extending their capabilities beyond pure text generation.

Troubleshooting the first call

Encountering issues during the initial setup is common. Here are some troubleshooting tips for your first GroqCloud API call:

  • API Key Issues:
    • Invalid Key: Ensure your GROQ_API_KEY is correctly copied and pasted. Each key is a long alphanumeric string.
    • Environment Variable Not Set: Verify that the environment variable is correctly set in your terminal session before running the script. Close and reopen your terminal if necessary, or explicitly set it for each new session.
    • Key Revoked/Expired: Check your GroqCloud console to see if the API key is still active. Keys can be revoked manually or, in rare cases, expire.
  • Network Errors:
    • Connectivity: Ensure your machine has an active internet connection and can reach api.groq.com.
    • Firewall/Proxy: Corporate firewalls or proxies might block outbound requests. Consult your network administrator if you suspect this is the case.
  • Model Errors:
    • Invalid Model Name: Double-check the model name specified in your request (e.g., llama3-8b-8192). Refer to the Groq supported models list for exact names.
    • Model Access: Some models might have restricted access or be in preview. Ensure your account is authorized to use the chosen model.
  • Request Payload Issues:
    • JSON Formatting: For cURL or direct HTTP requests, ensure your JSON payload is correctly formatted and valid. Tools like online JSON validators can help.
    • Required Parameters: Verify that all mandatory parameters, such as messages and model, are included in your request as per the Groq Chat Completions API reference.
  • SDK-Specific Errors:
    • SDK Version: Ensure you are using a recent version of the Groq SDK. Outdated SDKs might have compatibility issues.
    • Dependency Conflicts: If you encounter Python or JavaScript dependency errors, try reinstalling the SDK in a clean virtual environment (Python) or by clearing node_modules and reinstalling (JavaScript).
  • Check Groq Status Page: Occasionally, service outages can occur. Check the official Groq status page (if available) for any ongoing incidents.