AI with 200k Context Window: Call Claude 3 (Antropic AI) via API if you have an API key and format JSON

Hi everyone, if you want to place an API call to Claude from Anthropic, here is a snippet that works - it took me several hours and help from support to figure out how to re-format the JSON in Text Blaze, so I hope this will help anyone looking for the same thing:

  • You need to paste your own working API key, otherwise it will provide an error. (Details on how to obtain one: Getting access to Claude )

  • It needs some processing time, wait for the rotating arrows on the gray bar to stop. The example should return a 4 line poem in about 4 to 10 seconds once you pasted a valid API key

API Key: {formtext: name=APIKey; default=Enter YOUR API key here} {note}Please ensure the API Key is accurately entered. {endnote}
Temperature: {formtext: name=Temperature; default=0.7}
Max Tokens: {formtext: name=MaxTokens; default=4000}
Model: {formmenu: claude-3-opus-20240229; default=claude-3-sonnet-20240229; claude-3-haiku-20240307; name=Model} (Haiku = fastest, Opus = better details, Sonnet = in between)

{note} {formparagraph: name=Prompt; cols=40; rows=10; default=Please create a 4 line poem about ice cream}{endnote}
{formtoggle: name=Generate text; default=no; trim=left}{myModel=Model}
{myMessages=[[ "role": "user", "content": {=Prompt} ]]}
{urlload:; method=POST; headers=x-api-key:{=APIKey}, anthropic-version:2023-06-01, content-type:application/json; body={=toJSON([ "model": myModel, "max_tokens": MaxTokens, "temperature": Temperature, "messages": myMessages ], "{ model: string, max_tokens: number, temperature: number, messages: {role: string, content: string }[" & "] }")}; done=(contents, status)->["loading": no, "contents": fromJSON(contents), "status": status] }
{endformtoggle}{if: `generate text`=="no"}{error: Check "Generate text" to create the text; block=yes}{endif}
{if: `generate text`=="yes"}{note}PLEASE BE PATIENT. This might take a while:
There should be two gray arrows moving on the right side of the gray "URL-Call-Bar", indicating the AI is still working.
It could take up to 4:59 minutes (299 seconds) for complex tasks & long outputs with Opus (about 60 seconds with Haiku),
easy tasks with medium outputs should be finished in 3 to 15 seconds with Haiku (e. g. 9 seconds for a summary of a 12000 word text). {endnote} {endif}
Request Status:
URL Contents:

  • You can select the model and temperature, enter/modify the prompt and when you check the "generate" checkbox, it'll start calling the API (I used the "prompt" field + "generate" checkbox just for testing and call it with the clipboard content instead, with a fixed prompt in front)

  • API documentation: Create a Message

  • The maximum Output length is 4096 tokens (about 3000 words depending on the language), context window is 200000 tokes (about 150k words) and the maximum I tested was to summarize a 12000 (12k) word text which model Haiku completes in about 5-8 seconds.

  • Here is a list of models. Haiku is the fastest, it returns 400 lines of ice cream poem (560 words, 3300 characters) in 12 seconds. Opus is the most intelligent one, but it needs 65 seconds for a 200 line poem. At 500 lines, they've reached their output limit (but the input can be much longer, I processed srt-files with 3000 lines in 7 seconds for a summary).

Paste the desired model into "myModell":
Model Latest API model name
Claude 3 Opus claude-3-opus-20240229
Claude 3 Sonnet claude-3-sonnet-20240229
Claude 3 Haiku claude-3-haiku-20240307

  • When Posting any such snippet or any modification of this, be carefull:
    a.) At the end of the toJSON is a pair of square brackets that the forum software will "delete" when you paste it, preventing it from working. It took me 2 hours and the help of other users here to fix this, very hard to debug. Here's the solution for you and thanks @scott for your help:
    If you take a snippet that works perfectly inside textblaze and post it here, you have to replace the square brackets with ] " & " [ or the snippet looses the 2 square brackets at the end of the toJSON and won't work. Details see screenshots and discussion below.
    b.) The same for the ´ character in the if-statement, you have to replace it with \´

And one last thing: It seems like TextBlaze will strip any double spaces from any output if there is any HTML/Bold text in your snippet. This will destroy any structured list with sub-items AI models return as Markdown. The solution is here — remove ANY formatting from the snippet. But since this will make the snippet a lot harder to read IMHO, I left it like it is and linked to the solution here: What to do if TextBlaze removes double spaces needed for formatting. - simply use the 3-step-solution "when you don't need HTML code".




I seem to be getting a 400 error, did you encounter this at all? Have entered my own API key.

1 Like

Hi Keenan!
In short: Thanks for reporting, it's now fixed above. The forum reformatted the code. Here are the details and I hope this will be useful for anyone dealing with similar errors:

Yes, I had this in testing a lot. The 400 with "body is not valid JSON" occurs when something is missing from the toJSON function (e. g. the temperatue variable), if a format is off (e. g. not a string for Model or not a number for MaxTokens) or if a single error is in the special TextBlaze Syntax with it's use of {, [ and the other brackets.

I just copied the snippet back to my TextBlaze, indeed it did not work then. The forum software destroys some special characters during pasting, so a perfectly working TextBlaze snippet will break if it's not modified for posting. Thanks to @scott 's help, I now fixed it in the first post above.
I have also added more tweaking in the revised version above (e. g. an if statement with a message to wait for the AI) .

Here are the details for anyone else using this or a similar snippet, in case you want to post a modified version here later — to prevent the same from happening to you: somehow the copy paste to a snippets breaks some of the code.

Here is what you need to change to make it work, these two things seem to get broken when I post the forum snippet:

  1. the single quotation marks in the if statements (should be easy to spot) <<--- just fixed in the snippet above, thanks @scott , you have to write them with a backslash in front when posting here. The screenshot above is how it looks in textblaze and when pasting, but for the forum it needs to be a single \ added in front of each ` to receive a \`(in front of and after "generate text" inside the if-statement, see blue arrows on the left.

  2. the 2 square brackets at the end of the toJSON function , between the last two closing } :


It works perfectly fine like this in textblaze, but for the forum to make the code post here, you have to re-format the 2 brackets to [" & "]

The forum has issues with ` as they are used to apply code formatting in the forum: like so.

If your snippet contains back-ticks you need to put a single \ in front of each one so the forum doesn't try to use it for code formatting.

For example, you should replace `long name` as \`long name\`.

1 Like

Thanks @scott ! And how to fix the square brackets it cuts off? (see image above)

I think the forum software is trying to make them a checkbox.

This isn't great, but string concatenation should work. So instead of "...}[] ..."

Use: "...}[" & "] ..."

Thanks again and unfortunately, that does not fix the square brackets, They then get inserted with the full double quotations and the plus sign (and this again creates an error).
Sorry, I have to log out and do some client work now.

My apologies, you need to use the "&" concatenation operator, not the addition operator "+".

Here is an example:

{="... [" & "] ..."}

I've corrected the comment above.

1 Like

Thanks a lot @scott , I started to question my IQ and degree in computer studies :wink:
Now I see the tricky reformatting that was required, applied it to the initial post, and it works great after pasting this back to TextBlaze from the forum.

Let's keep the discussion about the details here to save anyone else the trouble when they use the toJSON function and paste a snippet here.