> For the complete documentation index, see [llms.txt](https://docs.stacks.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.stacks.co/cookbook/clarity/example-contracts/counter.md).

# Counter

{% code title="counter.clar" lineNumbers="true" fullWidth="false" expandable="true" %}

```clarity
;; An on-chain counter that stores a count for each individual

;; Define a map data structure
(define-map counters
  principal
  uint
)

;; Function to retrieve the count for a given individual
(define-read-only (get-count (who principal))
  (default-to u0 (map-get? counters who))
)

;; Function to increment the count for the caller
(define-public (count-up)
  (ok (map-set counters tx-sender (+ (get-count tx-sender) u1)))
)
```

{% endcode %}

## Contract Summary

The Counter contract is a simple on-chain counter that maintains individual count values for each principal address. This minimalist contract demonstrates fundamental Clarity data structures and state management patterns.

**What this contract does:**

* Stores a separate counter value for each user (principal)
* Provides read-only access to retrieve any user's count
* Allows users to increment their own counter by 1
* Initializes counters at 0 for new users automatically

**What developers can learn:**

* How to use `define-map` to create key-value storage in Clarity
* Pattern for per-user state management using principal addresses
* Using `default-to` to handle missing map entries gracefully
* Simple read-only vs public function distinction
* Basic arithmetic operations and map updates with `map-set`
* The `tx-sender` context variable for identifying callers


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.stacks.co/cookbook/clarity/example-contracts/counter.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
