EN ▾
Get API key

Groq API Guide: High-Performance Inference

The Groq API delivers ultra-low latency LLM inference using custom LP64 silicon, making it ideal for applications where speed is critical. This guide covers how to obtain your API key, integrate with popular SDKs, and understand the pricing structure for high-performance text generation.

Groq API Guide: High-Performance Inference

Updated

What is Groq API?

The Groq API provides access to high-performance large language model inference powered by Groq's custom LP64 inference chips. Unlike traditional GPU-based inference, Groq's architecture processes tokens sequentially with minimal latency, often delivering results in milliseconds rather than seconds. This makes it particularly suitable for applications requiring real-time responses, such as interactive chatbots, code completion tools, or agents that need to make rapid decisions based on model outputs.

The API follows the OpenAI-compatible endpoint structure, meaning you can use standard libraries like openai or langchain with minimal configuration changes. By simply pointing your client to Groq's base URL and providing your API key, you can use this speed without rewriting your entire application logic. It is important to note that Groq serves specific open-weight models optimized for their hardware, so model availability may differ from other providers.

Why Use Groq for LLMs?

Speed is the primary differentiator for Groq. Standard GPU inference often involves overhead from memory bandwidth and parallel processing coordination. Groq's deterministic execution allows for predictable token generation times, which is important for user experience in interactive applications. Faster inference reduces the time users wait for responses, leading to higher engagement and smoother interactions.

Beyond speed, the API offers consistent performance. Latency spikes are less common compared to shared GPU environments, providing a more reliable service level for production applications. This consistency is valuable for applications that require strict response time SLAs, such as financial trading assistants or real-time translation services. Additionally, the ability to stream responses efficiently allows for immediate display of tokens as they are generated, further enhancing the perceived speed.

Getting Your Groq API Key

To start using the Groq API, you need an account on the Groq Cloud platform. Visit the Groq console and sign up using your email address or a supported SSO provider. Once logged in, navigate to the API keys section to generate a new key. It is best practice to store this key securely in environment variables rather than hardcoding it in your source files.

The API key typically follows a standard format and is required for all requests. You can rotate keys if needed, and each key can be monitored for usage and errors through the console dashboard. Ensure you keep your key private, as it grants access to your account's compute resources. Groq may also provide usage limits or quotas depending on your plan, which can be adjusted as needed.

Installing the Groq SDK

Integration is straightforward because Groq is compatible with the OpenAI Python SDK. You can install the library using pip:

pip install openai

Then, configure the client to point to Groq's base URL. In Python, this looks like:

from openai import OpenAI
client = OpenAI(base_url="https://api.groq.com/openai/v1", api_key="your-groq-api-key")

This configuration allows you to use the same methods (chat.completions.create) that you would use with OpenAI. The response objects will contain Groq-specific timing metrics, allowing you to monitor performance. You can also use the Groq JavaScript SDK if you prefer working in Node.js environments.

Making Your First Request

Creating a simple chat completion request is identical to using the OpenAI API. You specify the model, such as llama3-8b-8192 or mixtral-8x7b-32768, and provide a list of messages.

response = client.chat.completions.create(
  model="llama3-8b-8192",
  messages=[{"role": "user", "content": "What is the capital of France?"}]
)
print(response.choices[0].message.content)

This code snippet demonstrates a basic interaction. Groq's models support various parameters like temperature, max_tokens, and stop sequences. The response includes usage statistics, including prompt tokens and completion tokens, which are used for billing. You can also enable JSON mode for structured outputs if needed.

Streaming Responses in Groq

Streaming is supported and recommended for applications that display text as it is generated. Set stream=True in your request to receive a stream of chunks.

response = client.chat.completions.create(
  model="llama3-8b-8192",
  messages=[{"role": "user", "content": "Write a story."}],
  stream=True
)
for chunk in response:
  if chunk.choices[0].delta.content:
    print(chunk.choices[0].delta.content, end="", flush=True)

This approach reduces the time until the first token appears (TTFT) and provides a smoother user experience. Each chunk contains a portion of the generated text. Groq's low latency makes streaming particularly effective, as the gaps between tokens are minimal. You can also access usage data from the last chunk in the stream.

Cost and Pricing Model

Groq charges based on token usage. Pricing varies by model, with smaller models being cheaper and larger models costing more per million tokens. You are charged for both input and output tokens. For example, Llama 3 8B might cost $0.10 per million input tokens and $0.10 per million output tokens, while larger models like Llama 3 70B have higher rates.

There are no monthly subscription fees for the API itself; you pay only for what you use. This pay-as-you-go model is efficient for sporadic workloads. However, for high-volume applications, costs can accumulate quickly, so monitoring usage is essential. You can set up billing alerts in the console to avoid unexpected charges. Compare this with other options like uncensored api alternatives if you need specific model behaviors or pricing structures.

Groq vs. Other Uncensored APIs

While Groq excels in speed, other providers may offer different trade-offs. For instance, some APIs focus on uncensored models like uncensored llama or uncensored mistral, which may not be available on Groq. If your use case requires specific model behaviors or larger context windows, you might need to evaluate other options.

Groq's strength is raw inference speed on supported models. Other providers might offer better pricing for very large models or more diverse model selections. If you need an uncensored grok alternative or specific fine-tuned models, Groq's current catalog may not include them. Always verify model availability and capabilities against your specific requirements before committing to a provider.

Questions and answers

Does Groq support function calling?

Yes, Groq supports function calling (tools) for models that have been trained for it, such as Llama 3. You can specify tools in your request, and the model will return structured JSON output indicating which functions to call and with what arguments. This is useful for building agents that interact with external APIs or databases.

What is the context window size for Groq models?

Context window sizes vary by model. For example, Llama 3 8B typically supports 8,192 tokens, while Llama 3 70B supports 8,192 tokens as well. Some models like Mixtral 8x7B support up to 32,768 tokens. Check the specific model documentation on Groq's console for accurate limits.

Can I use Groq with LangChain?

Yes, Groq is fully compatible with LangChain. You can use the ChatGroq class in LangChain to integrate Groq models into your chains and agents. This allows you to use Groq's speed within the LangChain ecosystem for complex workflows.

Are there rate limits on the Groq API?

Yes, there are rate limits based on your account tier. These limits are typically expressed in requests per minute (RPM) and tokens per minute (TPM). You can monitor your usage and limits in the Groq console. If you need higher limits, you may need to upgrade your plan or contact support.

Your key is one form away

Create an account, copy the key, change the base URL. That is the whole setup.

Get API keyRead the docs