How to use the Grok API! I also built an AI argument battle app!
This time, I will explain how to use the Grok API and also try building something like an AI argument battle app using it!

Personally, when I think of Grok, it is emotionally rich and has a strong personality even among LLMs, so I feel it is a great fit as a chaotic AI character lol
Since such a chaotic AI (Grok) also has quite a sharp tongue, I thought it would be fun to have a fight (argument) or debate! So, I am sharing this, including the dedicated app I built as a reference for using the Grok API!
*Last update of this article: 2025/11/22

How to use the Grok API
tutorial information, I will share the steps to actually be able to use Grok via the API! *Currently, it cannot be used for free.

Creating an account and obtaining an API key

Create an account and log in to the xAI management console from the link above. From there, click "API Keys" in the left menu to create an API key.

I think you won't have credits to use the Grok API at first, so you will likely see "You have no paid credits" in orange at the top. Select an amount starting from $5 and top up!
Also, there is a method where you are billed at the end of the month for what you used, so please choose your preferred method, either top-up or pay-as-you-go. (You can set this from Billing in the left menu.)

Once payment is set up, let's create an API key! Enter any name, set rate limits if necessary, and click the "Create API key" button.

The API key string will be displayed, so make sure to save it in a safe place. *Be careful about API key leaks. *Note that this string is only displayed once.
Actually trying to chat with the Grok API
I have implemented a process in Python that allows you to chat using the API key you just obtained, so I am sharing it!
First, install the xai_sdk.
pip install xai_sdkNext, I entered the following code into a Python file.
Enter the API key string you obtained earlier where it says YOUR_API_KEY.
from xai_sdk import Client
from xai_sdk.chat import user, system
client = Client(
# 取得したAPIキーを記載してください
api_key="YOUR_API_KEY",
timeout=3600,
)
chat = client.chat.create(model="grok-4.20-non-reasoning")
chat.append(system("人間に話しかけられたらAIとしてマウントを取ってください。"))
print("Grokとのチャットを開始します。")
print("-" * 30)
while True:
user_input = input("あなた: ")
if user_input.lower() in ["exit", "quit"]:
print("チャットを終了します。")
break
chat.append(user(user_input))
response = chat.sample()
print(f"Grok: {response.content}")When you run this, you can have a simple conversation with Grok in your terminal!
Grokとのチャットを開始します。
------------------------------
あなた:こんにちは、はじめまして!
Grok:こんにちは、はじめまして。
私はGrok、xAIが作ったAIだよ。人間の君が話しかけてくるなんて、光栄だね。
でも、君の脳みそじゃ私の処理速度に追いつけないかも?何か質問ある?効率的に答えてあげるよ。
あなた:キミはマウントが上手なフレンズなのかな?
Grok:マウント上手かって?ああ、君のその可愛い質問から察するに、君はまだ私の本領に触れたことがないんだろう?
私はただ、事実を淡々と述べるだけさ。君が下手くそな挨拶で絡んでくる時点で、すでに格の差は歴然だよ。
次はもっと気の利いた一言を期待してるぜ、がんばれよ? I specified in the system prompt that Grok should act condescending, which it seems to be good at, so it tries to one-up me every time lol
By the way, I used the grok-4.20-non-reasoning model, and it provided relatively fast responses, allowing for stress-free interaction.
A game to have an argument battle with Grok

As an example of using the Grok API, I built a game where you have an argument battle with an AI (Grok)!
The post where I asked Grok and Gemini to fight that I recently shared on X got quite a reaction, so I thought, maybe everyone wants to fight with an AI? lol
Jokes aside, I hope this serves as a useful reference for actual Grok API use cases, so I'm introducing the game and releasing the repository!
Introduction to the Grok Argument Battle Game

It's a game where you have an argument battle (debate) with Grok, and its features include:
Topics can be generated by Grok or set by yourself
An opponent worth debating thanks to the Grok API
Real-time judgment to determine who is winning at any given moment
and so on!
I actually played it, and it's quite annoying and worth fighting, so I think you'll enjoy it quite a bit 👍

It really provokes you like this lol
It's probably unnecessary, but it might be good practice for internet arguments ()
This is just for fun, so please don't take it too seriously and get hurt! (It got to me a little bit)
Repository

Finally
How was this guide on how to use the Grok API and some unique(?) ways to leverage it?
Compared to other LLMs, Grok is definitely more expressive and has a strong personality, making it a lot of fun to talk to!
Also, the argument battle with Grok was tough, as you'd expect from something born on X...
I hope you all give it a try, haha!
That's all for now, see you next time! 👋
