Skip to content
Edit this page View source of this page

Send one question to a language model through its API

Draft: not yet validated

The AI assistant that drafted this tutorial ran every command in MESA JupyterLab on 2026-12-03 (model js2/muse-glimmer) and saw the results described. It sent each request once; another model, or another try, may answer in other words. No person has yet followed the page. Tell us what you find.

An agent talks to its language model through an API: it sends text to a web address and gets text back. Sending one request yourself shows what goes in and what comes out.

You will: have one reply from a model, and know where in it to find the answer, the model's name, and the token counts.

Time: 10 minutes. You need: steps 1 and 3 of Sign in and start an AI agent in a MESA app done in this app session. If you set up your key in this terminal, load it first: source ~/.config/aiverde/env.

Steps

  1. In the terminal, make a scratch folder, then check that the settings are loaded:

    mkdir -p ~/work/api-test && cd ~/work/api-test
    echo "${OPENAI_API_KEY:+key loaded}" "$OPENAI_BASE_URL" "$AIVERDE_DEFAULT_MODEL"
    

    You should see: key loaded, an address ending in /v1, and a model's name. The first part prints those two words and not the key. Never print the key itself.

  2. Ask the service which models your key may use:

    curl -s "$OPENAI_BASE_URL/models" -H "Authorization: Bearer $OPENAI_API_KEY" | python3 -m json.tool | grep '"id"'
    

    You should see: one line for each model; here, 14.

  3. Write the request to a file. Paste all eight lines at once and press Enter:

    cat > ask.json <<EOF
    {
      "model": "$AIVERDE_DEFAULT_MODEL",
      "messages": [
        {"role": "user", "content": "How many days are in February 2028? Answer with the number only."}
      ]
    }
    EOF
    

    You should see: the prompt return. Run cat ask.json: the model's name is filled in, and the file holds no key.

  4. Send it:

    curl -s "$OPENAI_BASE_URL/chat/completions" -H "Authorization: Bearer $OPENAI_API_KEY" -H "Content-Type: application/json" -d @ask.json | python3 -m json.tool
    

    You should see: a block of JSON. Find three parts in it:

    • choices, then message, then content: the answer, 29.
    • model: the model that answered.
    • usage: prompt_tokens for what you sent and completion_tokens for what came back; here, 72 and 161.

    This model also returned reasoning_content, the text it wrote on the way to the answer. Those words are counted too, which is why 29 cost 161 tokens.

  5. Change the question and send again:

    sed 's/How many days.*only\./What did I just ask you?/' ask.json > ask2.json
    curl -s "$OPENAI_BASE_URL/chat/completions" -H "Authorization: Bearer $OPENAI_API_KEY" -H "Content-Type: application/json" -d @ask2.json | python3 -m json.tool
    

    You should see: a content that cannot name February. Here it quoted the new question back and said there was no earlier one. This call carried one message, and the model has nothing else.

Check it worked

You can point to the answer, the model, and the two token counts in the first reply, and the second reply shows no memory of the first. A chat or an agent seems to remember only because it sends the earlier messages again with each call.

If it didn't

Authentication Error, No api key passed in. The settings are not loaded in this terminal. Run source ~/.config/aiverde/env and start again from step 1.

Step 1 prints an empty line. The same cause, or aiverde-setup has not been run in this app session.

An error that names the model. Your key may not use the saved model. Put a name from step 2 in ask.json in its place.

Next

Not yet validated by a person. Drafted with AI assistance on 2026-12-03; every command was run in MESA JupyterLab that day.

Machine-readable versions of this page: Markdown twin · raw source on GitHub · llms.txt · llms-full.txt (whole site). See For AI agents.