# COBOL functions that answer questions about text

A copybook and called programs over the C library, for GnuCOBOL.

## Install

| Channel | Note |
| --- | --- |
| `thinkthen-cobol-VERSION-TARGET.tar.gz` | The copybook and the called programs, from each release. |
| `thinkthen-c-VERSION-TARGET.tar.gz` | The C library it calls, from the same release. |

Set `THINKTHEN_API_KEY` in the environment.

## A first call

```
identification division.
program-id. first-call.
environment division.
configuration section.
repository. function all intrinsic.
data division.
working-storage section.
copy "thinkthen.cpy".
01 engine usage pointer.
01 question pic x(200).
01 question-length usage binary-double unsigned.
01 evidence pic x(200).
01 evidence-length usage binary-double unsigned.
procedure division.
    call "thinkthen_engine_new" returning engine

    move "Does the customer ask for a refund?"
        to question
    move "Please refund my order. It arrived broken."
        to evidence
    perform ask-it
    if not outcome-yes
        stop run returning 1
    end-if

    move '{"decide": "Does the customer ask ' &
        'for a refund?", "threshold": "0.2:0.8"}'
        to question
    move "I want to send this back." to evidence
    perform ask-it
    if not outcome-not-sure
        stop run returning 1
    end-if
    stop run returning 0.

ask-it.
    move length(trim(question trailing))
        to question-length
    move length(trim(evidence trailing))
        to evidence-length
    call "TT-DECIDE" using engine
        question question-length
        evidence evidence-length
        tt-deadline-ms tt-answer tt-facts tt-failure.
```

*Build and run it beside the unpacked archives, thinkthen-c/ and thinkthen-cobol/*

```
cobc -x -free \
    -fstatic-call -fno-gen-c-decl-static-call \
    -I thinkthen-cobol/copybooks \
    -A "-include $PWD/thinkthen-c/include/thinkthen.h \
    -Wno-incompatible-pointer-types \
    -Wno-implicit-function-declaration" \
    -o first-call first-call.cob \
    thinkthen-cobol/src/tt_decide.cob \
    thinkthen-cobol/src/tt_error.cob \
    -L thinkthen-c/lib -lthinkthen \
    -Q "-Wl,-rpath,$PWD/thinkthen-c/lib"
./first-call
```

Not sure comes back as `outcome-not-sure`. Branch on it and send those cases to a person.

## Run facts

`TT-DECIDE` fills `tt-facts` with this call's run facts as JSON text. `TT-JSON-MEMBER` reads one member.

## Errors

`tt-failure` holds the kind as a code from 1 to 6, the retry flag and the message. Its level-88 names, such as `failure-backend`, test each kind.

## Settings and recording

`TT-ENGINE-NEW` takes the settings as JSON. `"record"` writes a recording to a folder. `"replay"` answers from that recording with no connection. The [Settings](/install/settings/) page lists every setting.

## Jev, Liquid d1 and Ollama from COBOL

COBOL reads its backend from the environment. Set `THINKTHEN_BACKEND` to the name below. Set the backend's key variable in the shell, and the same sample asks that backend.

| Backend | Name | Key variable |
| --- | --- | --- |
| [TypeSafe Jev](/install/backends/typesafe/) | `typesafe` | `TYPESAFE_API_KEY` |
| [Liquid d1](/install/backends/liquid/) | `liquid` | `LIQUIDAI_API_KEY`, then `LIQUID_API_KEY` |
| [Ollama](/install/backends/ollama/) | `ollama` | `OLLAMA_API_KEY`. A local address needs no key. |

*Ask TypeSafe Jev.*

```
export THINKTHEN_BACKEND=typesafe
export TYPESAFE_API_KEY=...
```

*Ask Liquid d1.*

```
export THINKTHEN_BACKEND=liquid
export LIQUIDAI_API_KEY=...
```

*Ask Ollama. This block names a second port, 11535, as the site's runs do. On the default port, leave THINKTHEN_BASE_URL out.*

```
export THINKTHEN_BACKEND=ollama
export THINKTHEN_BASE_URL=http://localhost:11535/v1
```

```
identification division.
program-id. backends.
environment division.
configuration section.
repository. function all intrinsic.
data division.
working-storage section.
copy "thinkthen.cpy".
01 engine usage pointer.
01 refund-question pic x(200)
    value "Does the customer ask for a refund?".
01 question pic x(200).
01 question-length usage binary-double unsigned.
01 evidence pic x(200).
01 evidence-length usage binary-double unsigned.
procedure division.
    call "thinkthen_engine_new" returning engine

    move "Please refund my order. It arrived broken."
        to evidence
    perform ask-it
    if not outcome-yes
        stop run returning 1
    end-if

    move "Thanks for the quick help yesterday!"
        to evidence
    perform ask-it
    if not outcome-no
        stop run returning 1
    end-if
    stop run returning 0.

ask-it.
    move refund-question to question
    move length(trim(question trailing))
        to question-length
    move length(trim(evidence trailing))
        to evidence-length
    call "TT-DECIDE" using engine
        question question-length
        evidence evidence-length
        tt-deadline-ms tt-answer tt-facts tt-failure.
```

*Build and run it beside the unpacked archives, thinkthen-c/ and thinkthen-cobol/*

```
cobc -x -free \
    -fstatic-call -fno-gen-c-decl-static-call \
    -I thinkthen-cobol/copybooks \
    -A "-include $PWD/thinkthen-c/include/thinkthen.h \
    -Wno-incompatible-pointer-types \
    -Wno-implicit-function-declaration" \
    -o backends backends.cob \
    thinkthen-cobol/src/tt_decide.cob \
    thinkthen-cobol/src/tt_error.cob \
    -L thinkthen-c/lib -lthinkthen \
    -Q "-Wl,-rpath,$PWD/thinkthen-c/lib"
./backends
```

The configuration file's `backend` names a default backend for every run. The [Configuration](/install/configuration/#configuration-file) page shows the file.

### OpenAI Decisions API from COBOL

OpenAI announced its Decisions API on 2026-09-29 in its [DevDay 2026 recap](https://openai.com/index/devday-2026-recap/). OpenAI has published no address, schema or price, so ThinkThen cannot call it. The [OpenAI Decisions API](/install/backends/openai/) page says what ThinkThen knows.

## What is different in COBOL

- Compile the programs you call from `src/` with your own, as the build line does with `TT-DECIDE`.
- The copybook names no, yes and not sure as level-88 conditions, so `if outcome-yes` reads the answer.
- `TT-DECIDE` writes into the question buffer, so set the question again before each call.

On GitHub: [github.com/botassembly/thinkthen](https://github.com/botassembly/thinkthen)
