BC3203

Setting up an AI assistant in RStudio

RStudio has a built‑in AI Assistant (the Posit Assistant pane) that can chat with you, explain code, and help debug. Before you can use it you must connect it to an AI provider – the service that actually runs the language model.

You are free to choose whichever provider you like. Several options are listed below. They all work the same way once connected; they differ mainly in cost and setup effort.

Option Cost Google/other account needed Setup effort
1. Posit AI Pass Free trial only (small credit, then paid) Posit account Easiest
2. Google Gemini Free (rate‑limited) Free Google account Medium
3. OpenRouter (free models) Free (rate‑limited) Free OpenRouter account Medium
4. Local model (Ollama) Free none Only if you run RStudio on your own computer

There is also a bonus option for code auto‑completion (GitHub Copilot) at the end – that is separate from the chat assistant and is free if you have a GitHub Student account.


Where the settings live

Open the Posit Assistant pane:

In the Assistant pane, the gear / settings menu is where you add a provider and enter your key. The Chat provider dropdown (also in Tools → Global Options → Assistant) is where you pick which connected provider is active.

The exact wording of menu items varies slightly between RStudio versions, but the flow is always: add provider → paste key → select it as the chat provider.


Option 1 – Posit AI Pass (easiest, but trial only)

Posit AI Pass is Posit’s own managed service. Setup is a single sign‑in – no API keys.

  1. Open the Posit Assistant pane.
  2. Make sure Chat provider is set to Posit Assistant / Posit AI.
  3. Click Sign In and complete the browser sign‑in (create a free Posit account if you don’t have one). No credit card is required.
  4. Start chatting.

Limitation: the free trial is a small one‑time credit allowance (a few dollars of usage), not an unlimited 30‑day period. When the credit runs out, continued use needs a paid subscription. Fine for trying the assistant out; not a long‑term free option for the course.


Option 2 – Google Gemini (free)

Google gives out free Gemini API keys. There are rate limits (roughly 15 requests/minute on the free tier) but that is plenty for one person doing coursework.

a. Get a free API key (about 2 minutes)

  1. Go to https://aistudio.google.com/apikey
  2. Sign in with a Google account (your university Google account works, or any personal one).
  3. Click Create API key → Create API key in new project.
  4. Copy the key (it starts with AIza…). Treat it like a password – don’t share it or commit it to a repository.

b. Connect it in RStudio

  1. Open the Posit Assistant pane → gear / settings menu → the option to configure / add language model providers.
  2. Choose Google Gemini.
  3. Paste your API key and save.
  4. Set the Chat provider dropdown to Google Gemini.
  5. For the model, pick a current Flash model. Use gemini-3.6-flash or newer – the gemini-2.5 (and older) models are no longer supported and will fail if selected.

Option 3 – OpenRouter (free models)

OpenRouter is a gateway to many models, including a rotating set of free ones.

  1. Create a free account at https://openrouter.ai.
  2. Go to https://openrouter.ai/keys and Create Key. Copy it (starts with sk-or-…).
  3. In RStudio: Posit Assistant pane → gear / settings → add provider → OpenRouter → paste the key.
  4. Set the Chat provider dropdown to OpenRouter.
  5. Choose a model whose name ends in :free (for example a free Llama, Gemini, or DeepSeek variant – the exact list changes over time).

Notes: free models have low rate limits, can be slow at busy times, and prompts sent to them may be used by the model providers for training. Don’t paste anything private or assessable.


Bonus – GitHub Copilot for code auto‑completion

If you have a GitHub Student Developer Pack, you get GitHub Copilot Pro for free. In RStudio this gives you inline code suggestions as you type (grey ghost text you accept with Tab). It does not provide chat – that still needs one of the providers above.

  1. Get free Copilot access via https://education.github.com (apply with your student email).
  2. In RStudio: Tools → Global Options → Assistant.
  3. Set Use code assistant to GitHub Copilot.
  4. Click Apply, then close and reopen the Options window (a known quirk – the Sign In button doesn’t appear until you reopen it).
  5. Click Sign In, copy the code shown, visit https://github.com/login/device, and authorise.

You can run Copilot completions and a chat provider (e.g. Gemini) at the same time.


Troubleshooting