> For the complete documentation index, see [llms.txt](https://docs.urbit.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.urbit.org/urbit-os/kernel/khan/example.md).

# Khan Example

Khan thread runner example - running threads from Gall agents with success/failure handling and result processing.

Here we'll look at a simple example of running a thread from a Gall agent via Khan. The Gall agent will take a boolean poke. If it's `%.y`, the thread will succeed and return some text. If it's `%.n`, the thread will fail with an error message.

Here's the thread, which you can save in the `/ted` directory of the `%base` desk on a fake \~zod:

#### `mythread.hoon`

```hoon
/-  spider
=,  strand=strand:spider
=,  strand-fail=strand-fail:libstrand:spider
^-  thread:spider
|=  arg=vase
=/  m  (strand ,vase)
^-  form:m
?.  !<(? arg)
  (strand-fail %i-have-failed 'foo' 'bar' 'baz' ~)
(pure:m !>('success!!!'))
```

Here's the Gall agent, which you can save in the `/app` directory of the `%base` desk on a fake \~zod:

#### `myapp.hoon`

```hoon
/+  default-agent
=+  ~
=*  state  -
^-  agent:gall
|_  bowl=bowl:gall
+*  this  .
    def   ~(. (default-agent this %.n) bowl)
++  on-poke
  |=  [=mark =vase]
  ^-  (quip card:agent:gall _this)
  ?>  ?=(%noun mark)
  =+  !<(succeed=? vase)
  :_  this
  [%pass /result %arvo %k %fard q.byk.bowl %mythread %noun !>(succeed)]~
::
++  on-arvo
  |=  [=wire sign=sign-arvo]
  ^-  (quip card:agent:gall _this)
  ?>  ?=([%result ~] wire)
  ?>  ?=([%khan %arow *] sign)
  ?:  ?=(%.n -.p.sign)
    ((slog leaf+<p.p.sign> ~) `this)
  ((slog !<(@t q.p.p.sign) ~) `this)
::
++  on-init   on-init:def
++  on-save   on-save:def
++  on-load   on-load:def
++  on-watch  on-watch:def
++  on-agent  on-agent:def
++  on-leave  on-leave:def
++  on-peek   on-peek:def
++  on-fail   on-fail:def
--
```

Let's try it out. First, we commit the files:

```
> |commit %base
>=
+ /~zod/base/31/app/myapp/hoon
+ /~zod/base/31/ted/mythread/hoon
```

Next, we'll start the agent:

```
> |start %myapp
>=
gall: installing %myapp
```

Let's try poking it with `%.y` so the thread succeeds:

```
> :myapp %.y
>=
success!!!
```

The thread succeeded and our Gall agent printed its result.

Now we'll try poking it with `%.n` so it fails:

```
> :myapp %.n
>=
khan-fact
i-have-failed
foo
bar
baz
[mote=%thread-fail tang=~['i-have-failed' 'foo' 'bar' 'baz']]
```

Khan automatically prints the error message, and our app has also pretty-printed the `$goof` it received.

***


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.urbit.org/urbit-os/kernel/khan/example.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
